1. Install initialization and Get methods
- The first part of the Express official website is installed and initialized
The introduction of the code
Const express = require('express') // introduce express const app = express() // instantiate const port = 3000 // set port // set URL with get // app.get('/', (req, res) => res.send('Hello World! ') // App.listen (port, () => console.log(' Example app listening on port ${port}! `))Copy the code
Nodeapp.js starts localhost:3000 and Cannot GET/because the URL routing and HTTP request were not processed
App.get ('/', (req, res) => res.send('Hello World! '))Copy the code
Restart Node app.js and access it on port 3000
Print Hello World!
2. Middleware POST delete
- Middleware is a custom extension of Express functionality
Perform a specific action before a request or response
App.use (express.json()) // Extend express functionality with JSON middleware
- Test the POST function
Const express = require('express') // introduce express const app = express() // instantiate const port = 3000 // set port App.use (express.json()) // Extend express functionality with JSON middleware // set url with get // The front part is the root route and the back part is the callback function and sends data to the client app.get('/', (req, res) => res.send('Hello World! ')) app. Post ('/', (the req, res) = > {/ / post a new request to the console. The log (' received the request body: 'the req. Body); Res.status (201).send() // Set the request success status code 201}) () => console.log(`Example app listening on port ${port}! `))Copy the code
-
Using Postman pictures can not paste
Choose the post
The address is localhost:3000
Body to select raw
- Note The postman data type must be SET to JSON; otherwise, the data cannot be retrieved after being sent
{“name”:” FHJ “}
Then click Send to see the status code 201 and the console prints out the JSON content
3. Put Updates data
App. Put ("/" id ", (the req, res) = > {/ / update the data to the console. The log (" receipt of a request parameter, id is: ", the req. Params. Id); Console. log(" Received request body :",req.body); res.send(); })Copy the code
It is important to restart the service after the change
Postman select PUT and fill localhost:3000/6 select Body raw json {“name”:” FHJ “}
Click Send to get the ID and JSON data
4. Delete Deletes data
App.delete ("/:id",(req,res)=>{// Delete data console.log(" received request parameters, id: ", req.params.id); res.status(204).send() })Copy the code
- Restart the service CTR +c node app.js
- So let’s test postman and say delete and say localhost:3000/6
- Inside the body, select None and hit Send
- Display 204 Console prints received request parameter, id: 6
Real Express projects will include multiple apis
Use the article example to create a/POST child route
- Create post.js on routes
const express = require('express'); // Router instance // Copy the previous four requests and modify the content // set the URL with get // The first part is the root route and the second part is the callback function and send data to the client route.get('/', (req, Res) = > res. Send ({id: 1, the title: "express introductory tutorial"})) / / sample data route. The post ('/', (the req, res) = > {/ / post a new request Console. log(' Save article :',req.body); res.status(201).send({id:2, ... Req.body}) // set request success status code 201}) route.put("/:id", (req,res)=>{// Update data console.log(" received request parameters, article ID: ", req.params.id); Console. log(" Received request body, new article content :",req.body); res.send({id:req.params.id, ... req.body}); }) route.delete("/:id",(req,res)=>{// delete data console.log(" received request parameters, article ID: ", req.params.id); res.status(204).send() }) module.exports = route; // Export routing variablesCopy the code
- You need to import this in app.js
const post = require('./routes/post')
app.use("/post",post)
Copy the code
- Post localhost:3000/post
And then I’m going to write some JSON data and I’m going to hit Send to get the ID and the JSON data
6. Simplify
- Create index.js under routes
const post = require("./post"); Exports =(app) => {// exports a function that receives app object mounts app.use("/post",post)}Copy the code
- How to use app.js?
const routes = require("./routes")
routes(app)
Copy the code
7. See here
- It may take longer than 3 minutes, but it should be very rewarding