Serving static files with restify

喜夏-厌秋 提交于 2019-12-03 14:35:19

问题


I am learning to use Node.js. Currently, I have a folder structure that looks like the following:

index.html
server.js
client
  index.html
  subs
    index.html
    page.html
res
  css
    style.css
  img
    profile.png
  js
    page.js
    jquery.min.js

server.js is my webserver code. I run this from a command-line using node server.js. The contents of that file are:

var restify = require('restify');

var server = restify.createServer({
    name: 'Test App',
    version: '1.0.0'
});

server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser());

server.get('/echo/:name', function (req, res, next) {
    res.send(req.params);
    return next();
});

server.listen(2000, function () {
    console.log('%s running on %s', server.name, server.url);
});

As you can see, this server relies on RESTIFY. I've been told I must use RESTIFY. However, I can't figure out how to serve static files. For instance, how do I server the *.html, *.css, *.png, and *.js files in my app?

Thank you!


回答1:


From the documentation:

server.get(/\/docs\/public\/?.*/, restify.serveStatic({
  directory: './public'
}));

But this will search files in the ./public/docs/public/ directory.
I prefer to use __dirname key here:

server.get(/\/public\/?.*/, restify.serveStatic({
    directory: __dirname 
}));

The value of __dirname is equal to script file directory path, which assumed to be also a folder, where is public directory.

And now we map all /public/.* urls to ./public/ directory.




回答2:


According to my current restify version (v5.2.0)

the serveStatic has been moved into plugins, so the code would be like this

server.get(
  /\/(.*)?.*/,
  restify.plugins.serveStatic({
    directory: './static',
  })
)

Syntax above will serve your static files on folder static. So you can get the static file like http://yoursite.com/awesome-photo.jpg

For some reason if you want to serve the static files under specific path like this http://yoursite.com/assets/awesome-photo.jpg for example.

The code should be refactored into this

server.get(
  /\/assets\/(.*)?.*/,
  restify.plugins.serveStatic({
    directory: `${app_root}/static`,
    appendRequestPath: false
  })
)

The option appendRequestPath: false above means we dont include assets path into the file name




回答3:


From Restify 7 the routes no longer take full regexes, so if you want /public/stylesheet.css to serve the file ./public/stylesheet.css, your code would now look like this:

server.get('/public/*', restify.plugins.serveStatic({
  directory: __dirname,
}))

This is because Restify 7 has a new (potentially faster) routing back-end: find-my-way




回答4:


this is how i'm serving static files in restify

server.get(/\/public\/docs\/?.*/, restify.plugins.serveStatic({

          directory: __dirname,

          default: 'index.html'

        }));

public access path will be : example.com/public/docs/index.html




回答5:


I came across this issue just recently, so while this may not help you it could help others who are having trouble with it.

When you declare Restify as const restify = require('restify');, the serveStatic method will be in the plugins object so using restify.serveStatic will quietly fail. The correct way to access the method is restify.plugins.serveStatic.

You can find the update docs here: http://restify.com/docs/plugins-api/#serve-static




回答6:


server.get('/', function(req, res, next) {
    fs.readFile(__dirname + '/index.html', function (err, data) {
        if (err) {
            next(err);
            return;
        }
        res.setHeader('Content-Type', 'text/html');
        res.writeHead(200);
        res.end(data);
        next();
    });
});



回答7:


Try this : Here view is a static resource directory name

server.get('/\/.*/', restify.plugins.serveStatic({

    directory: __dirname + "/view/",
    default: './home.html' 

   })
);


来源:https://stackoverflow.com/questions/19281654/serving-static-files-with-restify

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