0

So I have Node.js installed on my server.

However, I'm a little confused about the next bit. I've spent a few hours researching about Node.js but I can't seem to find any clearly-laid out info on what I'm looking for.

On my host PC, I can run Node.js through the command prompt and loading up localhost:port in the browser. This works fine. However, I can't figure out how this would translate onto using Node.js on my VPS. As I mentioned, it's installed. But how would I use physically use Node.js in my programs, and be able to access it on the pages when needed?

I'm usually a PHP developer but I'm using this chance to dive into the world of Node and pick it up, and begin using it instead of PHP.

Thank you for the help.

2
  • It depends whether a web server is already installed on your VPS or rather whether the port 80 is free. If it is free, you can run your node app with port 80. If not, run it with any other free port and instruct whatever web server is listening on 80 to forward any request to your desired port. Commented Aug 5, 2014 at 12:18
  • Running a Node app (or any other web application framework) directly on port 80 is considered bad practice. Since services running below port 1024 must run as root, you're giving full privileges to application code that may contain vulnerabilities. Instead, run a well-tested production-ready server such as Apache or Nginx on port 80, acting as a reverse proxy to a Node.js server only accessible from the same machine running on an unprivileged port. Commented Aug 5, 2014 at 12:37

1 Answer 1

1

Node.js is a little different to how you're used to using PHP, in that it doesn't replace PHP but PHP and Apache.

Your Node.js program is responsible for accepting every request and deciding how you respond to it. It isn't just something you can "access on the pages when needed", it serves the pages itself.

For example, instead of serving an index.php containing code to run when you visit http://example.com/, your Node.js server would contain code to run when you visit http://example.com, and decide how to handle the path /:

index.php

<?php
echo 'Hello world';

server.js

var http = require('http');
http.createServer(function(req, res) {
  if(req.url === '/') {
    res.end('Hello world');
  } else {
    res.statusCode = 404;
    res.end(req.url + ' not found');
  }
}).listen(3000);

Node.js is typically used with Apache or Nginx or [insert server here] as a reverse proxy, to forward external requests on to an internal Node.js server.

If all this sounds a little daunting, and you're thinking "why can't I just embed Node.js in pages like PHP", there's always PNP. However, it's probably not suitable for production usage, and at that point you might as not be using Node.js, as you won't have access to any asynchronous APIs, which includes pretty much everything.

I'd recommend the Node.js for PHP Developers blog series, which serves as a great introduction to the major concepts of Node.js.

Sign up to request clarification or add additional context in comments.

3 Comments

Okay, thank you for your comment! Very much appreciated :) But I do have another very noobish question, on my windows machine in order to run node.js I need to keep my command prompt running. How do I replicate this on the server? Do I need to do anything? Or will the code run anyway, without needing to keep something open (if you understand where I'm heading)? Thank you.
Node.js needs to be running to handle requests, it doesn't run per request like PHP. The standard way of keeping it running is by creating a system service that runs Forever or Supervisor.
Thanks again! I'll install that and hopefully see what happens. I appreciate your patience and help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.