Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Node.JS as REST server and web server

I am writing an application with Angular.js and Node.js.

There is a client-side app written in HTML & Angular.js that needs a web server like Apache to be online.

There is also a server-side REST webservice written in Node.js, built on restify (but I don't care which REST API I use, I can use another one).

I can get the whole thing working using a Node.js server for the REST webservice, and another Node.js server for serving the client-side webapp. But I'd like to have only one Node.js server running, on one URL/port (to prevent cross-domain AJAX requests).

How can I do so?

like image 489
Matthieu Napoli Avatar asked May 28 '13 19:05

Matthieu Napoli


People also ask

Can NodeJS act as a web server?

Node. js is an open source server environment.

Is NodeJS GOOD FOR REST API?

The standard they use for this is called Representational State Transfer (REST), and it works perfectly with the Node. js development techniques. Owing to this level of compatibility building a Node js REST API makes absolute sense.

Can I use both PHP and NodeJS?

Yes, and yes. Node and Apache / PHP can co-exist on a single server. The only issue you are likely to run into is that they cannot both listen on the same port. HTTP, by default, runs on port 80 and only one process can "listen" on a single port at any one time.


1 Answers

Not sure if this is applicable to your current problem - but app.use() in Express can let one main application set sub-applications to handle different route prefixes. So you could have your main application point any requests starting with /store/ to one Express app, and any requests to /app/ with a second Express app.

http://expressjs.com/api.html#app.use

like image 75
Plato Avatar answered Oct 01 '22 13:10

Plato