Noodjs (KOA) +mysql build server environment and basic additions and deletions to the operation

Source: Internet
Author: User

Create a project file manually

1. Under the specified directory, right-click to create the project folder.
2. Use the GIT init command to turn the contents of this file into a file that can be versioned by git.
3. Execute "npm init-y" command, create Package.json file, initialize
4. Execute the NPM init KOA command, install KOA, and a Package-lock.json file will be generated under the project path.

Create server

1. Create a app.js file, and then copy the following code in

const Koa = require(‘koa‘);const app = new Koa();const main = ctx => {    ctx.response.body = ‘Hello World‘;}app.use(main);app.listen(3000);

Then execute the following command

node app.js

Finally in the browser open http://127.0.0.1:3000/this connection, if you see the familiar "Hello world" that your server has been successfully started.

Connecting to a database

Here I am using the Mysql,mac on the MySQL installation can refer to my this Mac on the N-times install and uninstall MySQL.
Install the Nodejs MySQL package first

npm install mysql

To create a connection:

const Koa = require(‘koa‘);var mysql = require(‘mysql‘); //导入模块const app = new Koa();var connection = mysql.createConnection({   host:‘http://localhost‘,   user:‘root‘,   port: ‘3306‘,   password:‘‘,   database:‘test‘});connection.connect();  //创建连接
manipulating databases

Attach your own SQL statement to create the table

create table  record( id INT NOT NULL   AUTO_INCREMENT,userId INT NOT NULL,text TEXT,foreign key(userId) references user(id),PRIMARY KEY(id));create table record_img( id  INT NOT NULL   AUTO_INCREMENT,recordId INT NOT NULL,imgSrc varchar(255) NOT NULL,foreign key(recordId) references record(id),PRIMARY KEY(id));create table user( id INT NOT NULL   AUTO_INCREMENT,nickName varchar(200)  NOT NULL,gender varchar(200),city varchar(200),province  varchar(200),country  varchar(200),birthday DATE,PRIMARY KEY(id));

Basic operations on a database

    • Inquire
var sql =‘select * from user‘;connection.query(sql,function(err,result){    if(err){        console.log(‘[SELECT ERROR] - ‘,err.message);        return;    }    console.log(‘--------------------------SELECT----------------------------‘);    console.log(result);    console.log(‘------------------------------------------------------------\n\n‘);});
    • Modify
connection.connect();  //创建连接var sql =‘update user set nickName = ? where id = ?‘;var sqlpar=[‘julieCopy‘,1];connection.query(sql,sqlpar,function(err,result){    if(err){        console.log(‘[UPDATE ERROR] - ‘,err.message);        return;    }    console.log(‘--------------------------SELECT----------------------------‘);    console.log(‘UPDATE affectedRows‘,result.affectedRows);    console.log(‘------------------------------------------------------------\n\n‘);});connection.end();
    • Increase
connection.connect();  //创建连接var addSql =‘insert into  user (nickName) values(?)‘;var addPar= [‘jessicacopy‘];connection.query(addSql,addPar,function(err,result){    if(err){        console.log(‘[insert ERROR] - ‘,err.message);        return;    }    console.log(‘--------------------------SELECT----------------------------‘);    console.log(‘UPDATE affectedRows‘,result.affectedRows);    console.log(‘------------------------------------------------------------\n\n‘);});connection.end();
    • Delete
connection.connect();  //创建连接var delSql =‘delete from  user where id = 2‘;connection.query(delSql,function(err,result){   if(err){       console.log(‘[UPDATE ERROR] - ‘,err.message);       return;   }   console.log(‘--------------------------SELECT----------------------------‘);   console.log(‘UPDATE affectedRows‘,result.affectedRows);   console.log(‘------------------------------------------------------------\n\n‘);});connection.end();

Noodjs (KOA) +mysql build server environment and basic additions and deletions to the operation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.