Skip to content

Instantly share code, notes, and snippets.

@ShivamJoker
Last active July 4, 2023 09:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShivamJoker/c05a92c8a2a3185faa98e09744ef3700 to your computer and use it in GitHub Desktop.
Save ShivamJoker/c05a92c8a2a3185faa98e09744ef3700 to your computer and use it in GitHub Desktop.
Simple NodeJs server and commands to run it on port 80

Allow NodeJS to be run on port 80

sudo setcap cap_net_bind_service=+ep `readlink -f \`which node\``

Install pm2 to keep nodejs server running

npm i -g pm2

Start the NodeJS server with pm2

pm2 add server.mjs

Save the process to be loaded on reboot

pm2 save
pm2 startup
import http from "node:http"
const hostname = '0.0.0.0';
const port = 80;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader("Content-Type", "text/html");
res.end("<h1>Hello from LearnAWS.io</h1>\n");
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment