Last active
March 13, 2021 21:42
http-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
/* | |
description: | |
A simple http server, with a POST route that creates a txt file. | |
*/ | |
'use strict' | |
const http = require('http') | |
const fs = require('fs') | |
const HOSTNAME = 'localhost' | |
const PORT = 3000 | |
const server = http.createServer(requestHandler) | |
server.listen(PORT, HOSTNAME, () => { | |
console.log(`Listening into: http://${ HOSTNAME }:${ PORT }/`) | |
}) | |
function requestHandler(request, response){ | |
const { url, method } = request | |
const routes = { | |
"GET":{ | |
"/": function(req, res) { | |
res.setHeader('Content-Type', 'text/html') | |
res.write('<html>') | |
res.write('<head><title>Enter Message</title><head>') | |
res.write( | |
'<body><form action="/add-message" method="POST"><input type="text" name="message"><button type="submit">Send</button></form></body>' | |
); | |
res.write('</html>') | |
console.log('-route "/" acessed!-') | |
return res.end() | |
} | |
}, | |
"POST":{ | |
"/add-message": function(req, res){ | |
const body = [] | |
req.on('data', chunk => { | |
console.log(chunk) | |
body.push(chunk) | |
}); | |
return req.on('end', () => { | |
const parsedBody = Buffer.concat(body).toString() | |
const message = parsedBody.split('=')[1] | |
fs.writeFile('message.txt', message, err => { | |
res.statusCode = 302 | |
res.setHeader('Location', '/') | |
console.log('-route "/add-message" acessed!-') | |
return res.end() | |
}); | |
}); | |
} | |
} | |
} | |
function inexistentUrl(req, res){ | |
res.setHeader('Content-Type', 'text/html') | |
res.statusCode = 404 | |
res.write('<html>') | |
res.write('<head><title> Page not found </title><head>') | |
res.write( | |
'<body><h1>Page not found</h1></body>' | |
); | |
res.write('</html>') | |
console.log(`-page not found! url: ${url}`) | |
return res.end() | |
} | |
const responseFunction = routes[method][url] || inexistentUrl | |
responseFunction(request, response) | |
} |
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": "", | |
"main": "index.js", | |
"scripts": { | |
"start": "node index.js", | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"repository": { | |
"type": "git", | |
"url": "git+ssh://git@gist.github.com/0fbdb3beb753d46b8483052772a62365.git" | |
}, | |
"keywords": [], | |
"author": "gfsilva", | |
"license": "ISC", | |
"bugs": { | |
"url": "https://gist.github.com/0fbdb3beb753d46b8483052772a62365" | |
}, | |
"homepage": "https://gist.github.com/0fbdb3beb753d46b8483052772a62365" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment