How to set nodejs server on ubuntu 20.04
You can watch the youtube video too: https://www.youtube.com/watch?v=bju1c1CDEZk
To install or update nvm, you should run the [install script][2]. To do that, you may either download and run the script manually, or use the following cURL or Wget command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bashOr
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bashRunning either of the above commands downloads a script and runs it. The script clones the nvm repository to ~/.nvm, and attempts to add the source lines from the snippet below to the correct profile file (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completionnvm --versionnvm install node # Latest node js server versionOr
nvm install --lts # Latest stable node js server versionnode --versionnpm -vnvm lsnvm use 16.14.0nano app.jsconst http = require('http');
const hostname = '127.0.0.1'; // Your server ip address
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
if (req.url == '/') { //check the URL of the current request
// set response content
res.write('<html><body><p>This is Home Page.</p></body></html>');
res.end();
}else if (req.url == "/admin") {
// set response content
res.write('<html><body><p color="red">This is Admin Page.</p></body></html>');
res.end();
}else{
// set Invalid response content
res.statusCode = 401;
res.end('Page Not Found');
}
console.log(`New request => http://${hostname}:${port}`+req.url);
});
server.listen(port, '0.0.0.0', () =>{
console.log(`Server running at http://${hostname}:${port}/`);
});
node app.js