Home > Web Front-end > JS Tutorial > Node.js basic knowledge learning

Node.js basic knowledge learning

小云云
Release: 2018-02-27 09:14:46
Original
1251 people have browsed it

Javascript was generally used for web front-end development. However, due to the emergence of node.js, developing back-end programs with JavaScript is no longer a complicated matter. The js engine in node.js comes from the chrome v8 browser. With the additional tool code developed by node.js, it is very easy and efficient to use. In addition, node.js also has npm, a tool like pip. You can easily install third-party software using npm, which brings great convenience to our development work. It's really good to take advantage of the weekend to learn node.js.

1. Install node.js

sudo apt-get install nodejs

2. Install npm

sudo apt-get install npm

3. Try to install the express framework

npm install express --save

4. Write the simplest hello.js and use nodejs hello.js to execute

console.log('hello world')

5. More complex http server code


const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
 res.statusCode = 200;
 res.setHeader('Content-Type', 'text/plain');
 res.end('Hello World\n');
});

server.listen(port, hostname, () => {
 console.log(`Server running at http://${hostname}:${port}/`);
});
Copy after login

6. Use express to develop http server


var express = require('express');
var app = express();
 
app.get('/', function (req, res) {
  res.send('Hello World');
})
 
var server = app.listen(8081, function () {
 
 var host = server.address().address
 var port = server.address().port
 
 console.log("access url is http://%s:%s", host, port)
 
})
Copy after login

7. The remaining The following things

node.js has many frameworks and many third-party libraries. The project is very convenient to use. You are welcome to use it and practice more.

Related recommendations:

node.js basic module http, web page analysis tool cherrio to implement crawler_node.js

Node.js Installation and environment configuration tutorial

PHP and Node.js


The above is the detailed content of Node.js basic knowledge learning. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template