Introduction to the

Node.js believe everyone is not strange, now many companies recruit front-end is also required front-end personnel must be able to node first say about the application scenario, the current company with Node is mainly used to do the middle layer with Node, of course, most companies, because the general company project has been formed, plus node personnel is not easy to recruit, So there’s very little that can be done completely in the background (which is perfectly fine).

The middle layer

A lot of people say that Node is suitable for the middle tier, so what is the middle tier? In plain English, it is to wipe the bottom of the front and back end. At first, it was the middleware concept of Java, but later, the background was not willing to do it. Then, there were many big and old projects in big companies, which could not be moved, and the person who wrote before had left.

preface

You have heard of some frameworks related to Node, Express, KOA, or some enterprise applications to egg, with the front-end server rendering of next, NuxT, etc. Why not directly talk about koA convenient things, because I have always felt that a strong tool, people are weak, frameworks are more than dogs. A few years for a mainstream, only the foundation of the academy, the framework is in fact changing.

Simple server

Nodejs is definitely a server, so everyone knows that any communication between the browser and the server is done using the HTTP protocol. So, in theory, writing a server requires a very deep understanding of the HTTP service, but if you’re interested, you can search for it and lose your hair. The Nodejs authors must have thought of this as well, so the NodeJS package also has HTTP related modules to use directly

Create a file

Nodejs needs to be installed in advance, you can download it directly from the official website

First let’s create a new project, create a server.js, note not Chinese, name is arbitrary, and write the code as it is now

const http = require('http');

http.createServer(() = >{
  console.log('New link')})Copy the code

Then we run the file nodeserver.js from the terminal. Notice that the command line is finished. So here’s the concept of a server, is it executed? The answer is no, a server needs to be running all the time, but the server we’re writing is over all of a sudden, because we still have one very important thing left to do, which is to listen

Monitoring – popular point is to open a supermarket, there needs to be a salesman waiting for someone to come to the checkout, monitoring is this meaning. To the professional point of view it is simpler, a server may run many servers, such as Web related, mail related, what game related, this time you need to specify a port, equivalent to you order takeout to tell others the number of the house, ok next we change this code

const http = require('http');

let server = http.createServer(() = > {
  console.log('New link');
});

// Listen on the port
server.listen(5000);
Copy the code

This port is arbitrary, but there are some common ports that might be taken up, such as the 80-Web service. If you have these services installed on your computer, it will give you an error, just change it, and then we will talk about how the ports coexist, which is called port forwarding

Then run our filenode server.jsThere’s nothing there, because he’s listening, and he opens his browser and he types in the url, righthttp://localhost:5000/Then look at the terminal and see console.log

Even though we have Console out now, you can see that the browser doesn’t have anythingIt just keeps going around. It’s very simple because our server is not returning anything. How is it going to return

http.createServer

CreateServer first introduces createServer. Its first call takes two parameters: request, input, request information, response, output, and server response

There are two methods in response, one is **write ** and one is end, that is, one is to write something and one is to end. Ok, let’s modify the code again

const http = require('http');

let server = http.createServer((req, res) = > {
  res.write("hello");
  res.end();
});

// Listen on the port
server.listen(5000);
Copy the code

Note that after modifying the file, you need to close the service and restart the terminal by pressing Control + C

Call again at this timehttp://localhost:5000/ You can see the content

Handle the request

Now that we can return data, let’s look at what’s in the request

const http = require('http');

let server = http.createServer((req, res) = > {
  console.log(req.url);
  res.write("hello");
  res.end();
});

// Listen on the port
server.listen(5000);
Copy the code

First let’s type out the request path, and then open the browserhttp://localhost:5000/1.jpg1.jpg will be printed in the terminal/favicon.ico this is the icon of the website, this is chrome automatically for you to request if we don’t want to see it can be a direct if return

Now let’s change the code again

const http = require('http');

let server = http.createServer((req, res) = > {
  console.log(req.url);
  switch (req.url) {
    case '/1.html':
      res.write('1.html');
      break;

    case '/2.html':
      res.write('2.html');
      break;
    default:
      res.write('404');
      break;
  }

  res.end();
});

// Listen on the port
server.listen(5000);

Copy the code

Then a visit to 1.html will appearIsn’t that easy

Of course, this is just a very simple example, as you may have heard or seen of koa-Routers or other routers that can router directly, get and post, render static files, API services that can go to the Controller and so on, That’s how it all works, but it’s all wrapped up, but we’ll talk about it later in this series.

What are you interested in, or what problems you can directly add my friends, we communicate together

This is a nodeJS series from zero to master, and it would be too long to put it all in one article. The forwarding mentioned above will be in a later nginx article. There are a lot of things involved later, including node process, log processing, data processing, with page buried point data writing, analysis, packaging framework, database and so on, interested partners can pay attention to

After this article comes fs, node file manipulation is very important. Link to this article – juejin.cn/post/684490…

After setting up a simple server, kennel people are more curious about how to handle requests, submitting get and POST data by themselves

Nodejs- Basic – HTTP – Processing requests juejin.cn/post/684490…

Focus on not getting lost oh ~~~~~~~

WeChat: Dyy916829411

qq: 916829411