Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

serving static files with restify (node.js)

Tags:

I have the following code:

app.js

[...]  server.get(/\/docs\/public\/?.*/, restify.serveStatic({   directory: './public' }));  server.listen(1337, function() {   console.log('%s listening at %s', server.name, server.url); }); 

And I have the following file structure

app.js public/   index.html 

So I try browsing:

http://localhost:1337/docs/public/index.html 

and I get

{   code: "ResourceNotFound",   message: "/docs/public/index.html" } 

I tried with several variations, but none of them seemed to work.

I'm sure it should be something pretty obvious I'm missing

like image 888
opensas Avatar asked Mar 17 '13 17:03

opensas


People also ask

How do I serve a static file in node js?

In your node application, you can use node-static module to serve static resources. The node-static module is an HTTP static-file server module with built-in caching. First of all, install node-static module using NPM as below. After installing node-static module, you can create static file server in Node.

What is Restify in node js?

restify is a node. js module purpose built to create REST web services in Node. restify makes lots of the hard problems of building such a service, like versioning, error handling and content-negotiation easier.

What is Restify server?

A restify server object is the main interface through which you will register routes and handlers for incoming requests.

What are static files in node js?

Static files are typically files such as scripts, CSS files, images, etc... that aren't server-generated, but must be sent to the browser when requested. If node. js is your web server, it does not serve any static files by default, you must configure it to serve the static content you want it to serve.


1 Answers

restify will use the directory option as a prefix for the entire route path. In your case, it will look for ./public/docs/public/index.html.

like image 142
robertklep Avatar answered Sep 25 '22 04:09

robertklep