How to serve a static folder in sails.js?

限于喜欢 提交于 2019-11-28 03:17:56

问题


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'));

回答1:


You can use custom middleware for express in sails by adding it to your config/http.js:

var express = require('express');
module.exports.express = {
  customMiddleware: function (app) {
      app.use(express.logger());
      app.use(express.compress());
      app.use('/app', express.static(process.cwd() + '/../client/www/'));
    }
};



回答2:


Off the top of my head, there's two choices:

  1. Create a symlink assets/app pointing to your destination. The resources should be accessible via http://your.host.com/app/* since that's the way Sails serves assets.
  2. There's still Express underneath Sails, you should be able to access it with sails.express.app and do your thing, let's say, from config/bootstrap.js:

    var express = require('express');

    sails.express.app.use(express.static(process.cwd() + '/app'));




回答3:


I'm using Sails.js v0.12.4. The http.js uses module.exports.http and not module.exports.express I did the following to serve up another folder like the existing /assets folder. In my example to serve up the app folder, replace the 'node_modules/bootstrap/dist' path with /app

In the config/http.js file I added

var express = require('express');

Then in the middleware object I added the express static module. I'm wanting to serve up the bootstrap assets contained in my node_modules folder.

bootstrapAssets: express.static('node_modules/bootstrap/dist'),

Then in the order array, I added the 'bootstrapAssets' in the order I wanted this middleware to run. Here is the full code:

var express = require('express');

module.exports.http = {

  middleware: {

    passportInit    : require('passport').initialize(),
    passportSession : require('passport').session(),

    bootstrapAssets : express.static('node_modules/bootstrap/dist'),


    order: [
      'startRequestTimer',
      'cookieParser',
      'session',
      'bootstrapAssets',
      'passportInit',
      'passportSession',
      'myRequestLogger',
      'bodyParser',
      'handleBodyParserError',
      'compress',
      'methodOverride',
      'poweredBy',
      '$custom',
      'router',
      'www',
      'favicon',
      '404',
      '500'
    ],

Now in my HTML I can access the the bootstrap css using the following:

<link rel="stylesheet" href="/css/bootstrap.min.css">



回答4:


You can serve your files simple like this:

var express = require('../node_modules/sails/node_modules/express');

module.exports.express = {
  middleware: {
    custom: true
  },

  customMiddleware: function (app) {
    app.use(express.logger());
    app.use(express.compress());
    app.use('/api/docs',express.static('assets/swagger-ui/dist/'));
  }
};

This is my config/express.js file




回答5:


You can use this for sails 0.12.3:

  • Install express to your sail: npm install express --save
  • After that, modify config/route.js

    module.exports.routes = { ... '/public/*': require('express').static('the-directory-that-contains-public-director') ... }

This will works. However, it is a bit ugly that you have to create a directory as a parent for your public directory. It is because the static middleware create by express will count the '/public/' prefix in calculating to path to the target files.




回答6:


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



来源:https://stackoverflow.com/questions/20543252/how-to-serve-a-static-folder-in-sails-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!