Preface:

Hello everyone, I am dongdong acya, from today, I will lead you to learn Node.js, I hope you can become a full stack engineer from a front-end engineer. Let’s unlock Node.js!

Nodejs

1. What is Nodejs?

Node.js is simply JavaScript running on the server. Node.js is a platform built around the Chrome JavaScript runtime. Node.js is an event-driven I/O server-side JavaScript environment based on Google’s V8 engine, which executes JavaScript very fast and performs very well.

2. Who is suitable for this tutorial?

If you’re a front-end engineer and you don’t understand dynamic programming languages like PHP, Python, or Ruby, and you want to build your own service, Node.js is a great choice. Node.js is JavaScript that runs on the server side, and if you’re familiar with JavaScript, you’ll easily learn Node.js. Of course, if you are a back-end programmer and want to deploy some high performance services, then learning Node.js is also a great choice.

3. What do you need to know before starting this tutorial?

Before continuing with this tutorial, you should know some basic computer programming terms. If you’ve learned Javascript,PHP, Java, etc., this will help you get to know Node.js programming faster.

Create express backend server

1. What is Express?

Express is a minimalist, flexible Web application development framework based on the Node.js platform. It provides a series of powerful features to help you create a variety of Web and mobile applications.

2. Download express scaffolding globally:

npm install -g express-generator

3. Create express project:

Express Project Name

4. Start the back-end server:

① Download dependency:

npm i

② Start the back-end server

npm run start

When you see this page, your Node service has been created and started successfully.

5. Understanding of express project structure:

①bin: start configuration file, change the running port number in WWW

Node_modules: Stores all project dependencies, just like Java repositories

③public: used to store static resource files, images, CSS, JAVASCRIPT files..

(4) Routers: Routing files are equivalent to Controller in SpringMVC and Action in SSH

⑤ Views: the place to store the page

⑥ Package. json: project dependencies and developer information.

⑦app.js: application core configuration file, project entry

6. Download Nodemon and automatically restart:

When we modify the Node code, we have to restart the Node server, which greatly affects our development efficiency. We need to download a tool nodemon that can help us automatically restart after we modify the code.

① Download the nodemon dependency package globally:

npm i nodemon -g

② Change the startup mode to Nodemon:

3. Use Express to experience four types of get, POST, PUT, and DELETE requests:

1. Get request:

If the request mode is not set, the default will be get request, such as some IMG, font style using media tag to request, the default will be GET request, in the address bar can be directly asked, if you want parameters, can be directly in the ULR? Back splice. We write a Git request in the user module that simulates an array and sends it to the client.

Get requests can concatenate parameters directly in the address bar, and then see the data returned by the server directly.

2. Post request:

We continue to write a POST request in the Users module. This request must have three parameters, or it will return an error to the client. Only when name,age, and city are passed will the request return a success. Post request is generally used for new data, but there are some data, related to security, using GET request is not desirable, so post request will also be used.

Post requests are not accessible in the browse address bar. You can try it, like this:

We need to use an interface test tool called Postman, which returns an error when we pass no or less required arguments:

We only return success if we pass name,age, and city

Let’s print the received generation on the server side

Open the terminal, we can see the parameters sent by the client, we can do some processing of these data, considering the cost of learning students, we will not talk about this article, we know the principle is OK.

3. Put request:

A PUT request is similar to a POST request. It is usually used to edit and update data. Let’s change the request mode and add a mandatory id.

Test with Postman and return edit success

Open terminal:

4. Delete the request:

The delete request is usually used to delete, so we can set a mandatory parameter ID.

Use postman to test the deletion

Open terminal:

How to solve cross-domain?

If we use a real front-end project to tune this interface, we will find that it is not tunable and will report an error like this

The front end can also solve the problem of cross-domain. But this time we are focusing on the back end, so we will only focus on how the back end can solve the problem of cross-domain. So how does the back end solve the problem? Back-end resolution of cross-domain is as simple as setting cross-domain code in the request header.

Let’s look at the data returned by the request and see that the return was successful.

The front end renders the data to the page, perfect!!

The above is the general process of docking API interface between back-end engineers and us. The back-end is to operate the database and write some back-end business logic. And the front end is to write some page interaction logic, processing data rendering to the page.

Previous articles of the author:

Wechat pay is actually very simple, have not done wechat pay you have these doubts?

Small program Bluetooth unlock, how is the front end directly and hardware data interaction?

What is the lifecycle execution flow for parent and child components?