Skip to content

Instantly share code, notes, and snippets.

@AD0791
Last active June 14, 2020 18:47
Show Gist options
  • Save AD0791/efae6a7a55c858025548b3752cd49c4a to your computer and use it in GitHub Desktop.
Save AD0791/efae6a7a55c858025548b3752cd49c4a to your computer and use it in GitHub Desktop.
simple web server with http and http-status-code in nodeJS
// npm init
// npm i http -S
// npm i http-status-codes -S
const port = 9000;
const http = require('http');
const httpStatus = require('http-status-codes');
// app server
app = http.createServer((req, res) => {
console.log('Received an incoming request');
res.writeHead(httpStatus.OK, {
'Content-Type': 'text/html'
});
let resMessage = `
<style>
h1{
display: flex;
align-items: center;
justify-content: center;
}
</style>
<h1>Hello Node server</h1>`;
res.write(resMessage);
res.end();
console.log(`sent a response: ${resMessage}`);
});
// run server
app.listen(port);
console.log(`server is active: ${port}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment