NODEJS+EXPRESS 搭建最小服务器
var express = require('express');var http = require('http');
var fs = require('fs');
var app = express();
var path = require('path');
app.use(express.static(path.join(__dirname, 'public')));
app.get('/login', function(req, res, next) {
res.writeHead(200,{'Content-Type':'text/html'})
fs.readFile('./demo2.html','utf-8',function(err,data){
if(err){
throw err ;
}
res.end(data);
});
});
app.listen(3000);
console.log("监听端口 3000");
//console.log(app); 本次更新,增加了mysql相关代码
let express = require('express');
let http = require('http');
let fs = require('fs');
let app = express();
let path = require('path');
let mysql = require('mysql');
app.use(express.static(path.join(__dirname, 'public')));
app.get('/login', function(req, res, next) {
res.writeHead(200,{'Content-Type':'text/html'})
fs.readFile('./demo2.html','utf-8',function(err,data){
if(err){
throw err ;
}
res.end(data);
});
});
//主页输出 "Hello World"
app.get('/', function (req, res) {
console.log("主页 GET 请求");
res.send('Hello GET');
})
let config = {
host : 'localhost',
user : 'root',
password : '111111',
database : 'wp'
};
let pool = mysql.createPool(config);
let sql='SELECT * from users';
//输出 mysql users 表数据
app.get('/getusers', function (req, res) {
pool.getConnection((err, connection) => {
if (err) {
console.error(err);
} else {
connection.query(sql, (err, rows) => {
if (err) {
console.error(err);
} else {
res.send(rows);
}
connection.release();
});
};
});
});
app.listen(3000);
console.log("监听端口 3000");
页:
[1]