-
Notifications
You must be signed in to change notification settings - Fork 0
/
express.txt
52 lines (43 loc) · 1.39 KB
/
express.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
npm init
yarn add express
cat > app.js
var express = require('express'); //引入express模块
var app = express(); //创建express的实例
app.get('/', function(req, res){
res.send('Hello,myServer'); //服务器响应请求
});
app.listen(3000,function(){ //监听3000端口
console.log("Server running at 3000 port");
});
node app.js
yarn add mysql
cat > app1.js
var express = require('express'); //引入express模块
var mysql = require('mysql'); //引入mysql模块
var app = express(); //创建express的实例
var connection = mysql.createConnection({ //创建mysql实例
host:'127.0.0.1',
port:'3306',
user:'root',
password:'gemini',
database:'er'
});
connection.connect();
var sql = 'select tableid, schemaname, tablename, tabledesc from sys_tables';
var str = " ";
connection.query(sql, function (err,result) {
if(err){
console.log('[SELECT ERROR]:',err.message);
}
str = JSON.stringify(result);
//数据库查询的数据保存在result中,但浏览器并不能直接读取result中的结果,因此需要用JSON进行解析
//console.log(result); //数据库查询结果返回到result中
console.log(str);
});
app.get('/',function (req,res) {
res.send(str); //服务器响应请求
});
connection.end();
app.listen(3000,function () { ////监听3000端口
console.log('Server running at 3000 port');
});