13

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'));
umläute
  • 28,885
  • 9
  • 68
  • 122
fxp
  • 6,792
  • 5
  • 34
  • 45

7 Answers7

12

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/'));
    }
};
Community
  • 1
  • 1
Edwin Knuth
  • 143
  • 1
  • 6
6

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

bredikhin
  • 8,875
  • 3
  • 40
  • 44
  • I am having the same issue. I have index.html which is SPA (backbone and require.js)... how can I serve that within sails.js? – Amiga500 Jan 13 '14 at 17:55
  • http://stackoverflow.com/questions/21097679/confusion-about-web-application-ports/21099224#21099224 – bredikhin Jan 13 '14 at 19:01
  • I tried creating a new directory inside assets, but sails is not serving files that are in the new directory. – Kevin Wheeler May 06 '15 at 06:38
  • Nevermind. I didn't put an extension on the end of my filename so apparently the url was interpreted as a directory instead of a file and then failed because this nonexistent directory didn't have an `index.html` file in it. – Kevin Wheeler May 10 '15 at 04:01
  • looks like sails.express.app does not exist – eyn Aug 12 '21 at 04:28
  • @eyn yeah, this was for before v1, and it's been a while, so you should probably look at the docs for the version you're using – bredikhin Aug 12 '21 at 11:50
5

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">
jer0dh
  • 384
  • 3
  • 6
4

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

Dimas Crocco
  • 410
  • 6
  • 16
  • `dir` can be a relative path, so you can just do `serveStatic('foo', {skipAssets: true})` – bumbu Mar 18 '22 at 16:25
1

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

alvaropaco
  • 1,573
  • 18
  • 29
1

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.

Duc Vu Nguyen
  • 1,169
  • 9
  • 9
0

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

Kafka
  • 98
  • 6