Skip to content

Instantly share code, notes, and snippets.

@joshuaquek
Last active August 8, 2018 03:40
Show Gist options
  • Save joshuaquek/943e39e514611359af2b058284f29277 to your computer and use it in GitHub Desktop.
Save joshuaquek/943e39e514611359af2b058284f29277 to your computer and use it in GitHub Desktop.
Some code for a simple express server. Remember to install the respective npm modules too.
Summary: Some code for a simple express server. Remember to install the respective npm modules too.
// In the terminal, run:
// ` npm install dotenv body-parser express morgan `
// ...won't need to install `path` because its native in Node.
// ---- NPM Imports ----
require('dotenv').config()
const bodyParser = require('body-parser');
const express = require('express');
const logger = require('morgan');
const path = require('path');
// ---- Express Server Setup ----
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(logger('dev'));
// ---- To serve static files from `/public` ----
app.use('/', express.static(path.join(__dirname, 'public')));
// ---- Express Server Endpoints ----
app.get('/', (req, res) => {
res.json({ status: 'Server is running.' } );
});
app.listen(process.env.PORT || 3334); // Create a .env to set env variables
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment