We will create a small REST Api with express

Code on github https://github.com/kendarorg/Angular101


First let's install the library a project

npm install -g express-generator

Then create the dir demo002srv enter it and then the following to create the project and install express in the app. The cors module is needed to allow the Cross Origin requests to the server.

npm init
npm install express --save
npm install cors --save

Finally you can create a new file index.js that contains the listener. The app.options is needed to respond to cors option requests from the client UI

var express = require("express");
const cors = require('cors');
var port = 4201;
var app = express();

app.options('*', cors());
app.listen(port, () => {
    console.log("Server running on " + port);

And then you can start listening. But going to http://localhost:4201 will return an error since no handler had been set!

node index.js

Last modified on: February 17, 2020