Express Notes

Adding Express to Node

Add this to the top to import the packages of Express:

const express = require('express');

Now, declare a variable which will hold the express function:

const app = express();

To use the methods, add this:

app.use((req, res, next) => { ~Body of server~ });

It is important to add this if it is not last so that the server knows to move on to the next line:

next();

This is very useful, because it sends back data to the frontend:

res.send('the information you want to send');

To export the Express code to the Server, use:

module.exports = app;

Now to access the express code from the Server, simply add:

const app = require(relative file path)

Add the following and delete the previous code which was present:

const server = http.createServer((app);

Lastly, add:

app.set('port' , the same valid locahost number)

How to fetch server data

In the app.module.ts, add this in the imports:

import { HttpClientModule } from "@angular/common/http";

Also add it to the imports array, in the same file.

Again, in the typescript which you want to import the server, add:

import { HttpClient } from "@angular/common/http";

Now initialize a private variable as parameter in the constructor like so in the typescript:

constructor(private http: HttpClient){}

Then, to send or receive data from the server, simply enter in the typescript:

this.http.get<type>(full server path);

To add the IMPORTANT Observable in the typescript;

.subscribe((DataFromServer) => { this.server = DataFromServer.NameOfExport});

Now, to disable CORS errors, we add another app.use which has next() and contains:

res.setHeader("Access-Control-Allow-Origin", "");

Also, to disable CORS errors, we add another line after it:

res.setHeader("Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept");

Another line after that one:

res.setHeader("Access-Control-Allow-Methods", "GET, POST, PATCH, DELETE, OPTIONS");

SUBSCRIBE SHOULD ALWAYS BE ADDED IN .ts FILES AFTER CALLING AN HTTP METHOD LIKE GET OR POST.

App. Post And Get

app.post and app.get handle incoming post and get calls respectively. See for more information: this link. We need to make a const variable of type bodyParser as follows:

const bodyParser = require("body-parser");

To add bodyparser to your express after variable is declared:

app.use(bodyParser.json());

Server Boilerplate With Express

const http = require('http');
const app = require(relative file path)

const port = 3000;
app.set('port' , port)
console.log('Success! Server listening on: http://localhost:3000');

const server = http.createServer((app);

server.listen(port);

Express Boilerplate (server.js)

const express = require('express');

const app = express();

app.use((req, res, next) => {                                        //Observable
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    res.setHeader("Access-Control-Allow-Methods", "GET, POST, PATCH, DELETE, OPTIONS");
    next(); 
});

app.use((req, res, next) => { 
    console.log('This is Express.');
    next(); 
});

app.use((access path), (req, res, next) => { 
    const posts = [
        { 
            id="This is an ID", 
            title="This is a Title", 
            content="Good Stuff"
        },
        { 
            id="This is another ID", 
            title="This is another fake Title", 
            content="Good Stuff 2"
        }
    ];
    res.json(posts);
});

module.exports = app;