Skip to content

Instantly share code, notes, and snippets.

View natmegs's full-sized avatar

Natalie Smith natmegs

View GitHub Profile
@natmegs
natmegs / server-plain.js
Created August 20, 2017 21:22
Basic Node server
var http = require('http');
// Creates new Server object
// and takes a callback as param.
// The callback is an event listener;
// When server emits particular event,
// this function will be called.
// Callback takes 2 parameters,
// request (type IncomingRequest)
// and response (type ServerResponse).
@natmegs
natmegs / server-html.js
Created August 20, 2017 21:24
Node Server responding with HTML (sync)
var http = require('http');
var fs = require('fs');
http.createServer(function(req, res) {
// Specify html content type
res.writeHead(200, { 'Content-Type': 'text/html' });
// Read contents of index.htm file synchronously,
// Saved as string in var html
var html = fs.readFileSync(__dirname + '/index.htm');
@natmegs
natmegs / index.htm
Created August 20, 2017 21:25
Basic html file used with Node Server
<html>
<head></head>
<body>
<h1>Jelly Filled Donut</h1>
</body>
</html>
@natmegs
natmegs / server-htmldynamic.js
Created August 20, 2017 21:27
Node Server responding with dynamic HTML by parsing html file as string
var http = require('http');
var fs = require('fs');
http.createServer(function(req, res) {
// Specify html content type
res.writeHead(200, { 'Content-Type': 'text/html' });
// Read contents of index.htm file synchronously,
// Saved as string in var html.
// Use 'utf8' param to specify encoding and ensure you are
@natmegs
natmegs / index2.htm
Created August 20, 2017 21:27
Dynamic HTML template for Node Server
<html>
<head></head>
<body>
<h1>{Message}</h1>
</body>
</html>
@natmegs
natmegs / server-htmlstreams.js
Created August 20, 2017 21:28
Node Server using streams to pipe html content to response (async)
var http = require('http');
var fs = require('fs');
http.createServer(function(req, res) {
// Specify html content type
res.writeHead(200, { 'Content-Type': 'text/html' });
// Replaced readFileSync and end with createReadStream.
// Read contents of index.htm and pipe to writeable stream res.
// Keeps buffer small, sends data a chunk at a time
@natmegs
natmegs / qs.css
Created August 21, 2017 19:15
Flex align columns in rows
/* THESE ARE USED TO ALIGN DASHBOARD COLUMNS */
.tenant-info {
margin-bottom: 10px !important;
flex: 1;
}
.tenant-info-inner {
height: 100%;
}
@natmegs
natmegs / app.js
Created August 25, 2017 05:27
Run through of setting up a basic node app with express
var express = require('express');
// Middleware to parse body of HTTP requests
var bodyParser = require('body-parser');
// Invokes function returned from require.
// Creates an Express application;
// express() returns Express object
var app = express();
@natmegs
natmegs / form.htm
Created August 25, 2017 05:28
HTML file used in express app.js example
<html>
<head>
<link href="assets/style.css" type="text/css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js" ></script>
</head>
<body>
<h1>Jelly Filled Donut</h1>
<form method="POST" action="/person">
First name: <input type="text" id="firstname" name="firstname" /> <br />
Last name: <input type="text" id="lastname"
@natmegs
natmegs / app.js
Last active August 26, 2017 21:53
Second express example, separating routes into different controller files
var express = require('express');
var app = express();
// Import the functions returned by apiController and htmlController
// to add endpoints/routes to app.
var apiController = require('./controllers/apiController');
var htmlController = require('./controllers/htmlController');
var port = process.env.PORT || 3000;