Hello World
In this section we will create and run our first express.js project, and use the simplest request response
Preparation
- The system environment of this book is windows10 64Bit
- Please install Node.js in your development environment in advance , this book uses node v16.17.1
- It is recommended to use VsCode as a development tool, which has a good code prompt function
- Install an api debugging tool in advance, such as postman , apipost , etc.
create project
- Create a folder as project root
- Download express dependencies
Execute in the root directorynpm install express
- Download js-text-chart dependencies
Execute in the root directorynpm install js-text-chart
, this is a js library for outputting character drawings, this book uses this library as a starter in every example - Create a new entry file in the root directory, such as index.js
- Introduce dependencies in the entry file
const express = require('express');
const evchart = require('js-text-chart').evchart;
create server
- First create an express instance
const app = express();
- create server
Let's mount the app instance we just created to a specified port, such as 8080
const server = app.listen(8080);
- Add a callback function after the server is running
It refers to a series of operations after the server runs successfully. It is often used to output information and initialization, etc. I print the basic information of the project here. I
use the inline arrow function "()=>{//...}", also Can be replaced with plain "function {//...}"
const server = app.listen(8080, () => {
let host = server.address().address;
let port = server.address().port;
let str = "EXPRESS-DEMO";
let mode = [ "close", "far", undefined ];
let chart = evchart.convert(str, mode[0]);
console.log(chart);
console.log("Server is ready on http://%s:%s", host, port);
})
- cross-domain policy
By default, interaction under different domain names may not be allowed. We make some basic settings for the request, allowing cross-domain and all request methods to format the request header, and set the content of the request header to json format
app.all("*", function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Content-Type");
res.header("Access-Control-Allow-Methods", "*");
res.header("Content-Type", "application/json;charset=utf-8");
next();
});
first interface
concept
The front-end and back-end interactions are performed on the interfaces exposed at the back-end, and different interfaces correspond to different url paths on the back-end ports. The front-end uses different methods (GET, POST...) to request the specified path. When the back-end has a definition to monitor a certain path interface in a certain way, it will obtain the data carried by the request, process the request, and return the result to the front-end
practice
Next, we'll add an interface that returns the welcome message with the server's root path (GET)
app.get('/', function (req, res) {
res.send('Hello World!');
});
In the above code, the app instance listens to the GET request of the localhost:8080/ path , and passes in 2 parameters, the first is the request, and the second is the response . Execution returns Hello World!res.send
test interface
Use the interface testing tool to access localhost:8080/ in the form of GET and try it! You will get a Hello World!
second interface
For the first interface, we simply accessed the interface without transferring any data. Next, we will create an interface that carries data with traditional path request parameters (eg: url?id=1)
practice
Define a GET form, the path is /get interface, pass in 2 parameters request and response, we pass in the callback function to req.query
get all the request parameters on the path, and use these parameters as the return value of the interface
app.get('/get', function (req, res) {
let requestParams = req.query;
res.send(requestParams);
});
test interface
Use the interface testing tool to access localhost:8080/get?id=1&name=evanp in the form of GET Try it! You will get these two parameters!