Do not use the command "sudo apt-get install nodejs", use instead a Github-repository of Chris/Lea: sudo add-apt-repository ppa:chris-lea/node.js sudo apt-get update sudo apt-get install python-software-properties python g++ make nodejs
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:
varexpress=require('express');varapp=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!
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