Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve a static folder in sails.js?

Tags:

sails.js

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'));
like image 277
fxp Avatar asked Dec 12 '13 12:12

fxp


People also ask

How to serve static files in Node JS?

To serve static files such as images, CSS files, and JavaScript files, etc we use the built-in middleware in node.js i.e. express.static. Setting up static middleware: You need to create a folder and add a file. For example, app.js, To run this file you need to run the following command. node app.js

What is the use of the sails public folder?

This is roughly equivalent to the "public" folder in express, or the www/ folder you might be familiar with from other web servers like Apache. Behind the scenes, Sails uses the serve-static middleware from Express to serve your assets.

What is the assets/folder in sails?

In Sails, these files are placed in the assets/ folder. When you lift your app, add files to your assets/ folder, or change existing assets, Sails' built-in asset pipeline processes and syncs those files to a hidden folder ( .tmp/public/ ). The contents of this .tmp/public folder are what Sails actually serves at runtime.

How does the static middleware work in sails?

It is important to note that the static middleware is installed after the Sails router. So if you define a custom route, but also have a file in your assets directory with a conflicting path, the custom route will intercept the request before it reaches the static middleware.


3 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

like image 78
Dimas Crocco Avatar answered Sep 20 '22 18:09

Dimas Crocco


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/'));
    }
};
like image 12
Edwin Knuth Avatar answered Oct 11 '22 00:10

Edwin Knuth


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

like image 6
bredikhin Avatar answered Oct 10 '22 22:10

bredikhin