Skip to content

Instantly share code, notes, and snippets.

@rlemon
Created September 25, 2018 18:21
Show Gist options
  • Save rlemon/87263b5ee8535e0d6ca0fdd49d463d94 to your computer and use it in GitHub Desktop.
Save rlemon/87263b5ee8535e0d6ca0fdd49d463d94 to your computer and use it in GitHub Desktop.
// get your dependancies
// read up on ES modules or CJS modules.
// but this here is CJS modules
const express = require('express');
const http = require('http');
const path = require('path');
const app = express(); // create your express app
const server = http.createServer(app); // create your server, and pass it the app as its options, this will let express use that servers incoming and outgoing channels.
// unlike php+apache you have to handle your own static files.
// so if someone requests yoursite.com/images/image.png you need
// to know to serve that.
// express.static to the rescue, it will expose everything in the directory
// you specify, good for images, css files, client side js files, etc.
app.use( express.static( path.join( __dirname, 'static' ) ) );
// start building your router
// this will accept a route, then a callback for your handler
// in this case '/' is yoursite.com/ root
app.get('/', function( request, response ) {
res.sendFile( 'index.html', {
root: 'static',
headers: {
'Content-Type': 'text/html'
}
}, errorHandler);
});
function errorHandler(error) {
// handle errors and such
}
server.listen( 8080 ); // make the server listen on a port.
@dev-trix
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment