Skip to content

Instantly share code, notes, and snippets.

@gh0st
Created June 28, 2017 16:53
Show Gist options
  • Save gh0st/45f63c9fbfae98a704523f313acfaa13 to your computer and use it in GitHub Desktop.
Save gh0st/45f63c9fbfae98a704523f313acfaa13 to your computer and use it in GitHub Desktop.
Setting up the server side APIs in a MEAN/angular-cli app
/* courtesy of https://scotch.io/tutorials/mean-app-with-angular-2-and-the-angular-cli */
const express = require('express');
const router = express.Router();
/* GET api listing. */
router.get('/', (req, res) => {
res.send('api works');
});
module.exports = router;
/* courtesy of https://scotch.io/tutorials/mean-app-with-angular-2-and-the-angular-cli */
// 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