Building the API

From the command line, install express, body-parser, and cors as dependencies via npm:

npm install express body-parser cors
If you use npm 5, you don't need to specify the -S or --save flag to save as a dependency in your package.json file.

Next, we will create a server.js file in the root folder of the Angular project which will hold all our API logic:

// server.jsconst express = require('express');const path = require('path');const http = require('http');const bodyParser = require('body-parser');const cors = require('cors');const app = express();// Get API routesconst route = require('./routes/index');// Parser for POST dataapp.use(bodyParser.json());app.use(bodyParser.urlencoded({ extended: false }));// Use CORSapp.use(cors()); ...

Get TypeScript 2.x for Angular Developers now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.