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

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "how to master the Node.Js process". 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!

1. Pre-project preparation:

Take the express framework as an example

Npmiexpress-generator-g// global installation express framework

Express-e// generates express application skeleton

Npmi// installation dependency

Npmstart// listens 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 built on Node.js,Connect,Express and Socket.io.

-- 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.

2, start the project

-- use bootstrap to complete the layout (or it is also convenient to use the bulma front-end css framework)

Put the widgets that need to be reused in a folder where I created a new commjs directory

Load the DOM structure in the constructor (object-oriented idea)

Modularize all the components that need to be reused, where they need to be introduced, and code reuse

3. Understand mvc

-- Controller Controller:

Is 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: call the corresponding method (function) in the controller according to the path set in the route

Res.body= > get the parameters passed in the post request

Res.query= > get the data in the get request

The business logic is basically in the control layer, and the corresponding functions in the model are called to pass the data to be passed as parameters.

-- Model Model

The Model (model) is the part of the application that handles the application's 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 returns a promise object, and the success or failure functions passed through the controller are called by Promise.then.

Then returned to the front end by the controller res.json

-- View (View)

Is the part of an application that deals with data display.

Views are usually created based on model data

4, front-to-back

As the front end, if we need to get the data from the back end, we have to send an ajax request, and if the file operation is involved, we have to use the post request.

Get different data through different url (set in app.js)

View-Controller-Model, rendering the page based on the returned data

This is still the MVC pattern, so the MVC process must be familiar.

5, function

Login:

When it comes to database operations, the result of the query is the Promise object

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

In the model, promise.then () decides to call the successful or failed function, which is passed to the controller callback, and the controller res.json returns to the front end of the message.

User login information needs to be saved: npmicookie-session--save

Configure cookie-session middleware in app.js

Check if you are logged in:

When the front end is loaded, the ajax request is sent. According to the response information, the login is judged and the login effect is rendered.

When you click exit, set req.session to null

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

6, about file upload

-- involving file upload, introduced on the server side (routing)

Multer module npmimulter--save

-- configure file upload, cv principles, and modify the location and naming rules.

In routing, add a file upload method before the callback function

Router.post (', upload,single ('file upload form name name'), fn)

-- the controller determines whether a file is uploaded (res.file)

If the useful variable stores the file path = > constfile= "/ the save path configured in the route /" + req.file.filename

7 the meaning of MVC

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.

"how to master the Node.Js process" content is introduced here, 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