Last active
June 14, 2020 18:47
-
-
Save AD0791/efae6a7a55c858025548b3752cd49c4a to your computer and use it in GitHub Desktop.
simple web server with http and http-status-code in nodeJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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