简体   繁体   中英

Separating RESTful services from main web site architecture using Node.js

I'm starting a new project and I'd like opinions on my idea for a server architecture. Here's the setup:

  1. There is a client app on many devices (phones, tablets). These all need to make RESTful connections to a server for data. This server already exists and is running Node.js and Express to serve JSON.

  2. I need to build a customer website that needs access to the same data as the other client devices. Users will log into this web site, interact with their account, etc. This server could run Node.js, Apache, or whatever works.

It seems to make the most sense to me to keep the RESTful server completely stateless and truly only answer web service requests. This means that I'll need to use a separate server (or drone) to run the web site, and to treat this server as another client to the RESTful server.

Am I overcomplicating this? Does it makes sense to just serve JSON (for the devices) and HTML (for browsers) from the same server, and to introduce user session state into that server?

Develop the customer website as its own thing and have it use the REST API like any other client.

It might be convenient to develop it as an express app and use it from the main REST server.

website/index.js:

var app = require('express')();
app.get('/', function(req, res) { res.send('<h1>Website</h1>'); });
module.exports = app;

Then in your main REST server:

...
app.use(require('../website'));
app.listen(...);

This lets you almost treat the website as a different app, but still host it from the same server. It would be easy to run the website as its own server later if needed.

I have created a separate project for this kind of situation. For reference follow this

https://github.com/rahpal/RestOnNode

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM