This Sample of a web server written in Node.js to setup/start project in raw Node.js.
const http = require('http');
const hostname = '127.0.0.1';
const port = (process.env.PORT || 3000);
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Sample Test\n');
});
server.listen(port, () => {
console.log(`Server running at port:${port}`);
});
> node [options] [v8 options] [index.js | -e "index"] [arguments]
In this case:
> node index
or
> npm start
not compulsary, to use .js extension while running node.js application.
const hostname = '127.0.0.1';
const port = (process.env.PORT || 3000);
-----------------------------
res.end('Sample Test\n');
-----------------------------
console.log(`Server running at port:${port}`);