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'));
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
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.
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.
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.
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
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/'));
}
};
Off the top of my head, there's two choices:
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.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'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With