Last active
February 28, 2020 22:05
-
-
Save munkacsitomi/b3b6171f67bfff0d4ed450ec84fb311b to your computer and use it in GitHub Desktop.
Simple Node.js Server
This file contains hidden or 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
const http = require('http'); | |
const httpStatus = require('http-status-codes'); | |
const port = 3000; | |
const app = http.createServer(); | |
const getJSONString = (obj) => JSON.stringify(obj, null, 2); | |
app.on('request', (req, res) => { | |
var body = []; | |
req.on('data', (bodyData) => { | |
body.push(bodyData); | |
}); | |
req.on('end', () => { | |
body = Buffer.concat(body).toString(); | |
console.log(`Request Body Contents: ${body}`); | |
}); | |
console.log(`Method: ${getJSONString(req.method)}`); | |
console.log(`URL: ${getJSONString(req.url)}`); | |
console.log(`Headers: ${getJSONString(req.headers)}`); | |
res.writeHead(httpStatus.OK, { | |
'Content-Type': 'text/html' | |
}); | |
let responseMessage = '<h1>Response message.</h1>'; | |
res.end(responseMessage); | |
}); | |
app.listen(port); | |
console.log(`The server has started and is listening on port number: ${port}`); |
This file contains hidden or 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
const http = require('http'); | |
const httpStatus = require('http-status-codes'); | |
const port = 3000; | |
const app = http.createServer((req, res) => { | |
console.log('Received an incoming request!'); | |
let responseMessage = '<h1>Hello Node Server!</h1>'; | |
res.writeHead(httpStatus.OK, { | |
'Content-Type': 'text/html' | |
}); | |
res.write(responseMessage); | |
res.end(); | |
console.log(`Sent a response : ${responseMessage}`); | |
}); | |
app.listen(port); | |
console.log(`The server has started and is listening on port number: ${port}`); |
This file contains hidden or 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
{ | |
"name": "simple-node-server", | |
"version": "1.0.0", | |
"description": "A simple server with Node.js", | |
"main": "main.js", | |
"scripts": { | |
"start": "node main.js", | |
"log": "node log.js", | |
"route": "node route.js" | |
}, | |
"author": "Tamas Munkacsi", | |
"license": "ISC", | |
"dependencies": { | |
"http-status-codes": "^1.4.0" | |
} | |
} |
This file contains hidden or 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
const http = require('http'); | |
const httpStatus = require('http-status-codes'); | |
const port = 3000; | |
const app = http.createServer(); | |
const routeResponseMap = { | |
'/info': '<h1>Info Page</h1>', | |
'/contact': '<h1>Contact Page</h1>', | |
'/about': '<h1>About Page</h1>', | |
'/error': '<h1>Error Page</h1>' | |
}; | |
app.on('request', (req, res) => { | |
const routeResponse = routeResponseMap[req.url]; | |
res.writeHead(httpStatus.OK, { | |
'Content-Type': 'text/html' | |
}); | |
if (routeResponse) { | |
res.end(routeResponse); | |
} else { | |
res.end('<h1>Hello!</h1>'); | |
} | |
}); | |
app.listen(port); | |
console.log(`The server has started and is listening on port number:${port}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment