Permalink
Browse files

Initial commit, adds basic node server examples

  • Loading branch information...
kbbushman
kbbushman committed Jan 10, 2019
0 parents commit f97620e074ffcc2225e3ef3d879f4c7c25a600e1
Showing with 154 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +12 −0 index.html
  3. +1 −0 message.txt
  4. +13 −0 package.json
  5. +47 −0 routes.js
  6. +80 −0 server.js
@@ -0,0 +1 @@
node_modules
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Node Server</title>
</head>
<body>
<h1>Node Server</h1>
</body>
</html>
@@ -0,0 +1 @@
Test+Message
@@ -0,0 +1,13 @@
{
"name": "nodejs-server",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js",
"dev": "nodemon server.js"
},
"author": "Kenneth Bushman",
"license": "MIT"
}
@@ -0,0 +1,47 @@
const fs = require('fs');

const requestHandler = (req, res) => {
const { url, method } = req;

if (url === '/') {
res.setHeader('Content-Type', 'text/html');
res.write('<html>');
res.write('<head><title>My Node Server</title></head>');
res.write('<body><form action="/message" method="POST"><input type="text" name="message"/><button type="submit">Send</button></form></body>');
res.write('</html>');
return res.end();
}

if (url === '/message' && method === 'POST') {
const body = [];

req.on('data', (chunk) => {
console.log(chunk);
body.push(chunk);
});

return req.on('end', () => {
const parsedBody = Buffer.concat(body).toString();
// console.log(parsedBody);
const message = parsedBody.split('=')[1];
// writeFileSync is a blocking, synchronous function
// fs.writeFileSync('message.txt', message);
// writeFile is asnyc with a callback
fs.writeFile('message.txt', message, (err) => {
if (err) throw err;
res.statusCode = 302;
res.setHeader('Location', '/');
return res.end();
});
});
}

res.setHeader('Content-Type', 'text/html');
res.write('<html>');
res.write('<head><title>My Node Server</title></head>');
res.write('<body><h1>Hello from Node Server</h1></body>');
res.write('</html>');
res.end();
}

module.exports = requestHandler;
@@ -0,0 +1,80 @@
const http = require('http');

const routes = require('./routes');

const server = http.createServer(routes);

server.listen(5000);


// ======================= Additional Configs ======================= //

// SERVER WITH PLAIN TEXT RESPONSE
// http.createServer(function (req, res) {
// // writeHead takes status and MIME Type
// res.writeHead(200, {'Content-Type': 'text/plain'});
// res.end('Hello world\n');
// }).listen(3000, '127.0.0.1');

// SERVER WITH SYNCHRONOUS HTML RESPONSE
// http.createServer(function (req, res) {
// // writeHead takes status and MIME Type
// res.writeHead(200, { 'Content-Type': 'text/html' });
// var html = fs.readFileSync(__dirname + '/index.html');
// res.end(html);
// }).listen(3000, '127.0.0.1');


// SERVER WITH SYNCHRONOUS HTML RESPONSE AND DYNAMIC CONTENT TEMPLATE
// http.createServer(function (req, res) {
// // writeHead takes status and MIME Type
// res.writeHead(200, { 'Content-Type': 'text/html' });
// var html = fs.readFileSync(__dirname + '/index.html', 'utf8');
// var message = 'Hello world...';
// // HTML must be encoded to utf8 (turns it into string) for replace() to work
// html = html.replace('{Message}', message);
// res.end(html);
// }).listen(3000, '127.0.0.1');


// SERVER WITH HTML STREAM PIPED TO RESPONSE
// http.createServer(function (req, res) {
// // writeHead takes status and MIME Type
// res.writeHead(200, { 'Content-Type': 'text/html' });
// fs.createReadStream(__dirname + '/index.html').pipe(res);
// }).listen(3000, '127.0.0.1');


// SERVER WITH SYNCHRONOUS JSON RESPONSE
// http.createServer(function (req, res) {
// // writeHead takes status and MIME Type
// res.writeHead(200, { 'Content-Type': 'application/json' });
// var obj = {
// firstName: 'John',
// lastName: 'Doe'
// };
// // Serialize Data to JSON format
// res.end(JSON.stringify(obj));
// }).listen(3000, '127.0.0.1');

// SERVER WITH SYNCHRONOUS JSON RESPONSE AND ROUTING
// http.createServer(function (req, res) {
// if (req.url === '/') {
// fs.createReadStream(__dirname + '/index.htm').pipe(res);
// }

// else if (req.url === '/api') {
// // writeHead takes status and MIME Type
// res.writeHead(200, { 'Content-Type': 'application/json' });
// var obj = {
// firstName: 'John',
// lastName: 'Doe',
// };
// // Serialize Data to JSON format
// res.end(JSON.stringify(obj));
// } else {
// res.writeHead(404);
// res.end;
// }

// }).listen(3000, '127.0.0.1');

0 comments on commit f97620e

Please sign in to comment.