前端
<!DOCTYPE html>
<html>
<head>
<title>search</title>
<meta charset="utf-8">
<!--设置编码,防止乱码>
</head>
<body>
<form action="http://59.110.70.81:8080/process_get" method="GET">
<!--process_get与下文相关,ip不为127.0.0.1是因为要将信息发给远程服务器,必须有它的ip>
<br/>
标题:
<input type="text" name="title">
<input type="submit" value="Submit">
</form>
<script>
</script>
</body>
</html>
后端
var express = require('express'); //须安装express
var mysql = require('./mysql.js') //见第4篇的mysql.js
var app = express();
//app.use(express.static('public'));
app.get('/7.03.html', function(req, res) {
res.sendFile(__dirname + "/" + "7.03.html"); //如果请求的是7.03.html,向前端发送7.03.html即不断刷新原来的页面
})
app.get('/process_get', function(req, res) {//发送process_get,从mysql中调取信息发送给前端
res.writeHead(200, { 'Content-Type': 'text/html;charset=utf-8' }); //设置res编码为utf-8,向前端发送信息时,建议带上writehead
//sql字符串和参数
var fetchSql = "select url,source_name,title,author,publish_date from fetches where title like '%" +
req.query.title + "%'";//mysql语句
mysql.query(fetchSql, function(err, result, fields) {
console.log(result);//类似于之前的vals,向命令行中发送result
res.end(JSON.stringify(result));//向前端发送字符化的result
});
})
var server = app.listen(8080, function() {
console.log("访问地址为 http://127.0.0.1:8080/7.03.html")
})//8080端口,第二个参数回调函数
前端发送给7.03.html时
正常时
下一节讨论下如何用表格展示数据