Created
June 28, 2017 16:53
-
-
Save gh0st/45f63c9fbfae98a704523f313acfaa13 to your computer and use it in GitHub Desktop.
Setting up the server side APIs in a MEAN/angular-cli app
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
/* 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; |
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
/* 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