Skip to content

Instantly share code, notes, and snippets.

@parthghiya
Created January 31, 2017 10:25
Show Gist options
  • Save parthghiya/eb337e2ee63ce8785d714e251b7ef0b8 to your computer and use it in GitHub Desktop.
Save parthghiya/eb337e2ee63ce8785d714e251b7ef0b8 to your computer and use it in GitHub Desktop.
server js snippet for MEAN 2
// Get dependencies
const express = require('express');
const path = require('path');
const http = require('http');
const bodyParser = require('body-parser');
// Get our API routes
const api = require('./server/routes/api');
const app = express();
// Parsers for POST data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Point static path to dist
app.use(express.static(path.join(__dirname, 'dist')));
// Set our api routes
app.use('/api', api);
// Catch all other routes and return the index file
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
/**
* Get port from environment and store in Express.
*/
const port = process.env.PORT || '3000';
app.set('port', port);
/**
* Create HTTP server.
*/
const server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port, () => console.log(`API running on localhost:${port}`))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment