Preliminary learn how to build a server node

Recently it live at hand is not particularly large, so we looked at how to use nodejs stop and build a simple server, did not talk much directly to the question

1. First, set up your own node environment, how to build I do not say, and a lot of tutorials Baidu

2. Create a new folder structures were finished, in a new index.js file, as follows:

// 引入需要的包,注意在node中使用的是requireJs,所以不要使用import这种语法,不会被识别
const http=require('http');
const url=require('url');

// 创建服务器
http.createServer(function(res.req){
    var pathname=url.parse(req.url).pathname;// 获取请求的路径
    res.writeHead(200,{"Content-Type":'text/plain'}); // 设置响应头
    res.write('Hello Node'); // 响应内容
    res.end();
}).listen(8000);// 端口

 Folder on shift + right → → open a command window, enter the command node index.js, open the browser to access port 8000, you will see a response corresponding to the content

Or can perform different operations according to different contents in response pathname

At this point your node service has been launched

3. How to connect to the database

A complete background service must be able to access and manipulate the database, the next step is how to use the node to access the database:

First you have to rely npm install folder, open a command window to perform in your current folder: npm init, and then all the way round on the line

Then install the package mysql: npm install mysql --save

Then the original code to read:

// 引入需要的包
const http=require('http');
const url=require('url');
const mysql=require('mysql');

// 配置mysql数据库连接项
const connection=mysql.createConnection({
    host:'localhost', // 主机地址,默认localhost port默认3306
    user:'root', // mysql数据库用户名
    password:'123456', // 密码
    database:'testdb', // 数据库名称
})

// 连接数据库
connection.connect();

http.createServer(function(res,req){
    var pathname=url.parse(res.url).pathname;
    res.writeHead(200,{"Content-Type":'text/plain'});
    // 执行数据库查询操作
    connection.query('SELECT * FROM user',function(err,result){
        if(err){
            console.log(err);
            return;
        }
        console.log(result);
    })

    res.write('Hello World');
    res.end();
}).listen(8000);

console.log('Server has started');

After completion of the above code again in the command line execution node index.js, access to port 8000, then the command line will print the data in the database, then you have successfully connected to the database on behalf of the


The record about the recent problems encountered in the project development company:

DefaultValue in a form and receive only initialValue as first assignment, after the value will not be receiving, if you want to change, then:

For components may be used in the form of a value corresponding to change this.props.form.setFieldValue

May be used for the individual component value = {this.state.value} way binding to

 

Published 47 original articles · won praise 38 · views 60000 +

Guess you like

Origin blog.csdn.net/qq8241994/article/details/89404737