Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use node to set up background quickly

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use node to quickly build the background, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

1. Install node,express,express-generator first (version 4.x separates generator, so it needs to be installed separately)

two。 Enter the project file input command express project name, npm I install the response package, then the simple back-end framework will be set up for you. The default port is 3000 www modification under bin.

I am generally used to rewriting app.js and deleting the bin file, because the more concise the better.

Rewrite app

Var http=require ('http') var server=http.createServer (app) / / omit the middle and then delete the direct listening server.listen in the final export app (' 3030 listen, () = > {console.log ('server started successfully');})

Finally, modify package.json. Because of everyone's habit, I am used to enabling dev startup.

Modify in script scripts

"scripts": {"dev": "nodemon. / app.js"}

Notice here that I installed nodemon using hot module replacement, that is, real-time refresh.

two。 Install the database mysql

Then it is tested in index

Let's not talk about the detailed database operation.

3. The following is the secondary encapsulation of the route

First create an app/index.js file to route the request outside

/ / this is the const {exec} = require ('.. / unil/db') / * GET home page that handles the execution of database statements. * / init= (req,res) = > {exec ('select * from goods_type_info where 1 contacts, [], (err, result) = > {if (err) {console.log (' service link error');} else {res.send ({code:200000,data:result})})} module.exports = {init} / / then call let getDate=require ('.. / app/index') / * GET home page in the required file. * / router.get ('/', getDate.init); / / this is which route executes which statement this is the first to deal with the route, the second is the method to execute the route, and if the route is nested, it needs to be spliced.

The classification in this can be separated in a little detail, such as which page uses all the routes, and then imported uniformly.

In addition, the first template quickly created by expres does not need to be imported into body-parser because it is discarded, and the second is because it uses app.use (express.json ()); app.use (express.urlencoded ({extended: false})); instead of

Remember that the request header initiated at the front end is in this format, otherwise the data received at the back end has problems in different formats and has different writing methods, detailed Baidu Content-Type

In this way, a simple background framework can be built quickly.

I would like to add one point here, that is, if you build locally, you must write the port number when the front end is turned on, otherwise it is easy to create false cross-domain, resulting in the front-end request, the back-end holds the data, and cannot get the session on the second request.

Thank you for reading this article carefully. I hope the article "how to use node to build a background quickly" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report