How to serve a static folder in sails.js?

Viewed 11759

I want to serve a folder app, which is at the same level with assets folder, under the url /app.

It's possible with AppController to serve file according the url, but I want to know whether it is possible to do this like the following with express?

app.use(express.static(__dirname + '/public'));
7 Answers

In Sails 1.0 you can modify the file config/routes.js and add this:

var express = require('express')
var serveStatic = require('serve-static')
var os = require('os')

const dir = `${os.homedir()}/foo` // dir = /home/dimas/foo

module.exports.routes = {

  '/public/*': serveStatic(dir, {skipAssets: true}), // <-- ADD THIS

  '/': {
    controller: 'FooController',
    action: 'checkLogin'
  },

};

Then you have to create the directory structure:

/home/dimas/foo/public

NOTE that the public entry (the route) is INCLUDED in the filesystem path, the files to be served must be placed there!

After that you can access any content by hitting the following URL:

http://localhost:1337/public/foratemer.txt

Sails v1.0: Serve a file with a . dot folder in sails. Example: https://myWebSite.com/.well-known/test.txt

in the config/http.js file add express.static to serve and then add publicFolder in the order array.

module.exports.http = {
middleware: {

publicFolder: express.static('public/public'),

  order: [
      'cookieParser',
      'session',
      'publicFolder'
      // 'favicon',
    ],
}}

Create a public folder and .well-known folder so public/.well-known/test.txt

Related