Last active
August 8, 2018 03:40
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Summary: Some code for a simple express server. Remember to install the respective npm modules too. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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