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
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‘);});
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();
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();
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