admin管理员组

文章数量:1331673

So I did a REST Api in Node JS and now I have a blocker.

app.use('/api', router);

This code makes sure that every url is prefixed with api. Now what If I want to serve an HTML file when the path is just "/"? Should I create another "express" application? I'm just working in localhost right now and I have to do "node server.js" to launch the server. I can't really launch 2 servers at the same time, or can I? One for the api: server.js and one for the rest of the code: client.js (that's a bad name but whatever.). I'm confused on how I have to setup things...

Thanks you for the help.

So I did a REST Api in Node JS and now I have a blocker.

app.use('/api', router);

This code makes sure that every url is prefixed with api. Now what If I want to serve an HTML file when the path is just "/"? Should I create another "express" application? I'm just working in localhost right now and I have to do "node server.js" to launch the server. I can't really launch 2 servers at the same time, or can I? One for the api: server.js and one for the rest of the code: client.js (that's a bad name but whatever.). I'm confused on how I have to setup things...

Thanks you for the help.

Share Improve this question asked May 31, 2014 at 23:46 user1834464user1834464
Add a ment  | 

1 Answer 1

Reset to default 8

You can see that you use your api routes to, most likely, serve JSON content. Well, using the same mechanism you can configure your router to serve any other kind of content for a particular route.

So, if you would like to serve some HTML content for the root of your application, let's say a Wiki page or documentation of you API, it is as simple as:

app.get('/', function(req, res){
   res.render('index.html');
});

Of course, if you are just rendering a static html page, you might just as well configure a middleware to define where you place all your static content.

app.use(express.static(path.join(__dirname, 'www')));

Where www is the directory where you chose to put your index.html page.

Having said that, if what you built was a REST API, chances are that it is intended to be reused by multiple other applications, and therefore, it is customary to that the client applications are running independently of the REST API. Some client applications may not even be Web applications (i.e. Android, iOS, desktop apps, etc). So you should take that into account before thinking in developing a Web client within the same project.

But nothing prevents your from providing a default implementation of UI that consumes your REST API within the same project/server. And yes, it is possible to run more than one HTTP server serving different applications. There are considerations you might need to take into account if you use separate servers for your API (i.e. CORS).

If you decide to serve everything within the same application, you may want to make sure there is a clear decoupling in the design in a such a way that the client application consumes and uses the rest layer as if it was independent. You should isolate the routes for the REST layer from those used for your client in such a way that if, later on, you want to make the client APP run independently you would not have a problem with that.

How about something like express-namespace to help you organize your routes?

本文标签: javascriptNode JS REST APInow how do I serve HTML filesStack Overflow