Setup a Node.js Webserver - sitcomlab/IPED-Toolkit GitHub Wiki

Table of Contents

  1. Install Node.js
  2. Load the Node.js-Package "Express"
  3. Creating a webserver
  4. Configurate the webserver

1. Install Node.js

2. Load the Node.js-Package "Express"

  • We need to load the Node.js-Package "Express" to create a simple Webserver
  • First go to the directory of your application. In this folder you will install the packages. This is usefully, because you can use different packages in different applications and sometimes with different package-versions (our folder: ":∼/IPED Development"):
    sudo npm install express

3. Creating a webserver (Example)

  • Create a new JS-File named "server.js" on your local computer:
var express = require('express');
var app = express();

app.set("view options", {layout: false});
app.use(express.static(__dirname + '/public'));

app.get('/api-url/:id', function(req, res){
	res.send('Hello World' + req.params.id);
	console.log(req.params.id);
});

app.listen(8080);
console.log("App listens on http://localhost:8080");
  • Create a new folder named "public" and insert into it a new simple "index.html" for testing (see all files in the GitHub-Respository)
  • Upload the files via FTP into the folder: ":∼/IPED Development"
  • Change to your terminal/console and go to the folder: ":∼/IPED Development"
  • Start the Node.js-Server with
    sudo node server.js
  • To stop the Node.js-Server press keys "CTRL + C" or exit the console
  • Attention: the latest version is stored at GitHub! The code could be changed!

4. Configurate the webserver

  • Source: http://github.com/nodejitsu/forever
  • Because the webserver can only start by your command in the terminal/console it depends on your connection. So when you exit the console or exit the Node.js-Console to make a other command it shutdown the server. So it is usefully to install a package called "Forever", which runs the server automatically without a Node.js-Console.
  • sudo npm install forever -g
  • So you don't need to start the Node.js-Server with the command "node server.js", you can use the command
    sudo forever start server.js
  • If you want to stop the server, use the command
    sudo forever stop server.js
  • For restarting, use the command
    sudo forever restart server.js
⚠️ **GitHub.com Fallback** ⚠️