kukunut0 kukunut0 - 8 months ago 39
Node.js Question

A node.js server for both web and mobile app

I am creating a game by Unity and I want to upload the players' score to MongoDB. Therefore, I have built a node.js server listening to port 3000, and the scores will be sent to the server and store into the database.

My question is that if I want to create a website for viewing/analyzing players' scores, which approach should I use?

  1. create two node.js servers, one for the web, one for the game

  2. one node.js server but listen to port 80 and 3000 (im not sure whether it is possible or not)

  3. any other better suggestions?

Thank you.


I would create one Node server, one to serve both api and web requests.

It sounds like the data served by the API and the web will be the same or subsets of each other. So you'll probably want to share code, lookup the same stuff from the database, etc etc.

From here, you could either create separate routes that the api uses and the web uses (/api/v1/my_scores vs /my_scores) OR realize that you're just asking for different representations of the same data and do something RESTful like checking the accept header and either sending server rendered HTML or sending JSON back to the client.

Alternatively, you could just create a api in Node, then use a purely front end tool like Angular or React to create a web front end for your site.