In this section we are going to work with REST, JSON and NodeJS-Express.
Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine.
Node.js package ecosystem, npm, is the largest ecosystem of open source libraries in the world.
Basically... it's a framework used for scalable server-side and networking applications.
Is a Node.js web application server framework, which is designed for building single-page, multi-page, and hybrid web applications.
It has become the standard server framework for node.js. Express is the backend part of something known as the MEAN stack (MongoDB, ExpressJS, AngularJS, NodeJS).
Install:
mkdir express-demo
cd express-demo
npm init --yes
npm install express
First web server:
- Create the
index.jsfile.
const express = require('express');
const app = express();
app.get('/', function (req, res) {
res.send('hello world')
});
app.listen(3000);- In your current folder run
node index.js- Open http://localhost:3000 in your browser and a hello world message should appear.
Representational State Transfer -> Architectural style.
It is a coordinated set of architectural constraints that restrict the roles / functions of architectural elements and the permitted relationships between those elements within any architecture that fits that style.
- Client / Server
- Stateless: The status of the resource is handled by the client, never by the server.
- Uniform interface (HTTP)
Methods:
- POST (C)
- GET (R)
- PUT (U)
- DELETE (D)
- Create the
index.jsfile. - Install express running
npm install express - In your
index.jsfile
Setup the server.
const express = require('express');
const app = express();
app.set('port', 5000);
app.use(express.json());
let products = [];Configure CORS.
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,POST');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
next();
});Create post method.
app.post('/products', function (req, res) {
const product = {
name: req.body.name,
price: req.body.price,
description: req.body.description
}
products.push(product);
res.send(products);
});Finally listen. And run node index.js
app.listen(app.get('port'), () => {
console.log(`Server listening on port ${app.get('port')}`);
});JSON: JavaScript Object Notation.
JSON is a syntax for storing and exchanging data.
JSON is text, written with JavaScript object notation.
Data is represented in name/value pairs
{
"squadName": "Super hero squad",
"homeTown": "Metro City",
"formed": 2016,
"secretBase": "Super tower",
"active": true,
"members": [
{
"name": "Molecule Man",
"age": 29,
"secretIdentity": "Dan Jukes",
"powers": [
"Radiation resistance",
"Turning tiny",
"Radiation blast"
]
},
{
"name": "Madame Uppercut",
"age": 39,
"secretIdentity": "Jane Wilson",
"powers": [
"Million tonne punch",
"Damage resistance",
"Superhuman reflexes"
]
},
{
"name": "Eternal Flame",
"age": 1000000,
"secretIdentity": "Unknown",
"powers": [
"Immortality",
"Heat Immunity",
"Inferno",
"Teleportation",
"Interdimensional travel"
]
}
]
}- Build a Web server using express.js. Use the files from this content.
- Use the route
/submit-formto receive your form data values. Forms are received usingapp.post('/submit-form') console.logthereq.bodyvalues.- Start your node.js server
node index.js - Open
form.htmlfrom the2-dom-manipulationexercise using a liveserver. - Use your
fetch,async,awaitknowledge to submit the form to this URL http://localhost:3000/submit-form Remember the form data is usually sent through the POST method. - Type some data and submit the form.
- Check your terminal console, the values you sent in the form should now appear in your command line.