5.2 Hello Express

Install Express
npm install express --save


Basic Setup

const express = require('express');
var app = express();
app.get('/', (request, response)=>{
})

request: request information from user
response: methods available to response back to user


response.send('Hello Express');
When user accesses to our server, we send this string to them.


app.listen(3000);
This starts our server.


nodemon server.js


Alt text


Sending JSON file

app.get('/', (request, response)=>{
response.send({
name: 'Jude',
age: 19,
city: 'Waterloo'
});
});

Add Another Route

app.get('/about', (request, response)=>{
response.send("About Page");
});

Using app.get, we are able to specify any number of routes as we want.


Challenge: Create a Route ‘ /bad'

Solution:

app.get('/bad', (request, response)=>{
response.send({
code: `BAD_ACCESS`,
error: `You should not be accessing this`
});
});

Alt text