1.在指定目錄下,右擊建立項目文件夾。
2.使用git init 命令把此文件下內容變成可由git進行版本控制的文件。
3.執行「npm init -y」命令,建立package.json文件,初始化
4.執行「npm init koa」命令,安裝koa,此時項目路徑下會生成一個package-lock.json文件。html
1.建立一個app.js文件,而後將如下代碼複製進去node
const Koa = require('koa'); const app = new Koa(); const main = ctx => { ctx.response.body = 'Hello World'; } app.use(main); app.listen(3000);
而後執行如下命令mysql
node app.js
最後在瀏覽器打開http://127.0.0.1:3000/這個鏈接,若是看到熟悉的「Hello World」就說明你的server已經啓動成功啦。git
這裏我是用的是mysql,mac上mysql的安裝能夠參考個人這篇mac上n次安裝與卸載mysql。
先安裝nodejs的mysql包sql
npm install mysql
建立鏈接:數據庫
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(); //建立鏈接
附上本身建立表的sql語句npm
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) );
對數據庫的基本操做json
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();