前端
<!DOCTYPE html>
<html>
<head>
<title>search</title>
<meta charset="utf-8">
<!--设置编码,防止乱码>
</head>
<body> //form表单
<form action="http://59.110.70.81:8080/7.02.html" method="GET">
<!--不能写127.0.0.1因为我要访问远程服务器,必须要填它的ip>
<br/>
title:
<input type="text" name="title">
<input type="submit" value="submit">
</form>
<script>
</script>
</body>
</html>
后端
var http = require('http');
var fs = require('fs');
var url = require('url');
var mysql = require('./mysql.js');
http.createServer(function(request, response) {
var pathname = url.parse(request.url).pathname; //parse返回字符串
var params = url.parse(request.url, true).query; //parse返回对象
fs.readFile(pathname.substr(1), function(err, data) {
response.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' }); //请求头包括编码和文本类型
if ((params.title === undefined) && (data !== undefined))
response.write(data.toString());
else {
response.write(JSON.stringify(params));//write输出
var select_Sql = "select title,author,publish_date from fetches where title like '%" +
params.title + "%'";
mysql.query(select_Sql, function(qerr, vals, fields) {
console.log(vals);
});
}
response.end();
});
}).listen(8080);
console.log('Server running at http://59.110.70.81:8080/');
但是运行代码发生错误:address already in use :::8080
网上查询,得知8080端口被占用
先输入 netstat -lntp
再输入
sudo kill -9 1848
重新执行代码,即可