Node.js
Node.js runs single-threaded, non-blocking, asynchronously programming. This way it is very memory efficient. Here is how PHP handles a file request:
- Sends the request to the server
- Waits while the file system opens and reads the file
- Returns the content to the client
- Ready to handle the next request
Here is how Node.js handles a file request:
- Sends the request to the server
- Ready to handle the next request
- The server returns the content to the client
Install
$ sudo apt update
$ sudo apt install nodejs
$ sudo apt install npm #node package manager
$ nodejs -v
$ npm -v
In order for some npm packages to work you will also need to install the build-essential.
$ sudo apt install build-essential
Example
Create file myfirst.js and add the following code:
let http = require('http');
http.createServer(function (request, response){
response.writeHead(200, {'Content-Type': 'text/html'});
response.end("Hello Node.js");
}).listen(8000);
node myfirst.js
Now, your computer {{works as a server`!
http:// localhost:8000 will display "Hello Node.js"
Remove
$ sudo apt remove nodejs
$ sudo apt purge nodejs
You can remove any unused packages that were automatically installed.
$ sudo apt autoremove
Last update: 156 days ago