Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs')
- const handleRequest = (req, res) => {
- const url = req.url
- const method = req.method
- if (url === '/') {
- res.setHeader('Content-Type', 'text/html')
- res.write('<html><head><title>Home!</title></head>')
- res.write('<body><h1>Welcome Nameless!</h1><form action="/create-user" method="GET"><button>CREATE USER</button></body>')
- res.write('</html>')
- return res.end()
- }
- if (url === '/create-user' || method === 'GET') {
- res.statusCode = 200
- res.setHeader('Content-Type', 'text/html')
- res.write('<html><head><title>Home!</title></head>')
- res.write('<body><form action="/users" method="POST"><input type="text" name="username"><button type="submit">LOGIN</button></form></body>')
- res.write('</html>')
- return res.end()
- }
- if (url === '/users' && method === 'POST') {
- const requestBody = []
- req.on('data', chunk => {
- requestBody.push(chunk)
- })
- return req.on('end', () => {
- const parsedBody = Buffer.concat(requestBody).toString()
- const username = parsedBody.split('=')[1]
- res.statusCode = 200
- res.setHeader('Content-Type', 'text/html')
- res.write('<html><head><title>Home!</title></head>')
- res.write(`<body><h1>Welcome, ${username}!</h1></body>`)
- res.write('</html>')
- return res.end()
- })
- }
- res.statusCode = 302
- res.setHeader('Location', '/')
- res.end()
- }
- module.exports = handleRequest
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement