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 the Node.Js framework

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

Share

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

This article introduces the knowledge of "how to use the Node.Js framework". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Pre-project preparation:

Take the express framework as an example

Npm I express-generator-g / / globally install express framework express- e / / generate express application skeleton npm I / / install dependent npm start / / listen on port 3000

Expansion: at present, the most popular node frameworks on the market are:

Sail.js

Sails.js is like the Rails framework on the Node.js platform. This is a reliable and scalable development framework, a service-oriented architecture that provides data-driven API collections. It is very convenient to develop multiplayer games, chat applications and real-time panel references, and can also be used to develop enterprise-level Node.js applications. Sails.js is based on Node.js, Connect, Express and build.

Koa.js

Koa.js is the Web framework for the next generation of Node.js. Designed by the Express team. It aims to provide a smaller, more expressive and more reliable development foundation for Web applications and API.

Koa can greatly improve error handling by getting rid of callbacks through the generator. The Koa core does not bind any middleware, but provides an elegant set of ways to write server applications quickly and happily.

Project start

Use bootstrap to complete the layout (or it is convenient to use the bulma front-end css framework) to put the widgets that need to be reused in a folder. Here I have created a new commjs directory constructor to load the DOM structure (object-oriented idea) to modularize all components that need to be reused, where they need to be introduced, and code reuse.

Understand mvc

Controller Controller: the part of the application that handles user interaction.

Typically, the controller is responsible for reading data from the view, controlling user input, and sending data to the model.

Function: according to the path set in the route, call the corresponding method (function) in the controller, res.body= > get the parameters passed in the post request res.query= > get the data business logic in the get request is basically in the control layer, call the corresponding function in the model, and pass the data to be passed as parameters

The model Model Model (model) is the part of the application used to deal with the application data logic.

Role: usually model objects are responsible for accessing data in the database.

Because the functions in the model need to be called in the controller, you need to export the model at the end. In the model, operations involving databases depend on mogoose packages

The model processing data will return a promise object, and the success or failure functions passed through the Promise.then call controller will be returned to the front end by the controller res.json.

The View is the part of the application that deals with the display of data.

Views are usually created based on model data

Frontend to backend as frontend if we need to get backend data, we have to send ajax requests. If file operations are involved, we must use post requests to go through different url (set in app.js), get different data view-Controller-Model, and render the page here in MVC mode according to the returned data, so we must be familiar with the MVC process.

Function

Login: when it comes to the operation of the database, the result of the query is the Promise object, and the operation in the controller is still the same. The parameters passed from the front end to the controller, a successful callback, a failed callback model in which promise.then () decides to call the successful or failed function is passed to the controller callback, and the controller res.json returns to the information front end.

Need to save user login information: npm I cookie-session-configure cookie-session middleware in save app.js

Check whether to log in: when the front end loads, send an ajax request to determine whether to log in according to the response information, and set req.session to null when the login effect is rendered and clicked to exit

* * fromData.append (",") can append request information

About file upload

When it comes to file upload, the multer module npm i multer-save is introduced into the server (routing) to configure file upload, cv principle, modify the save location and naming rules to route, and the callback function is preceded by a file upload method router.post (', upload,single ('file upload form name name'). Fn) the controller determines whether there is a file upload (res.file) if useful variables store the file path = > const file= "/ save path configured in the route /" + req.file.filename

Mvc meaning

MVC layering helps manage complex applications because you can focus on one aspect at a time. For example, you can focus on view design without relying on business logic. It also makes it easier to test the application. MVC layering also simplifies grouping development. Different developers can develop view, controller logic and business logic at the same time.

This is the end of "how to use the Node.Js Framework". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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