我是node的新手.js和mySQL
var app=express();
app.listen(9999,function(){ //I'm using 9999 port and I use Localhost
console.log('Server is running');
})
//connect mySQL
var client=mysql.createConnection({
user:'root',
password:'pswd' //for Question
});
我使用9999端口并在localhost上打开服务器.
我有html.这只是测试html文件(minuk\u test.html)
--主要.js公司--
app.get('/store/restaurant_info',function(request,response){
client.query('SELECT * FROM RESTAURANT_INFO',function(error,result,fields){
if(error){
console.log('Query syntax error');
}else{
response.json(result);
console.dir(result[0].name);
console.log('check sending data');
}
});
});
app.get('/Project/html/minuk_test.html',function(request,response){
fs.readFile('../html/minuk_test.html','utf-8',function(error,data){
if(error){
console.log('Loading minuk_test.html is failed');
}
response.send(data);
})
})
这是我的html代码.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>API TEST</title>
<link rel="stylesheet" href="/minuks_test.js">
</head>
<body>
<h1>Test</h1>
<div id="restaurant">
<span></span>
<span></span>
</div>
</body>
</html>
这是我的javascript
--javscript文件--
import fetch from "node-fetch";
function getAPI(){
const url=`http://127.0.0.1:9999/store/restaurant_info`;
fetch(url)
.then(response=>{
console.log(response);
response.json();
})
.then(data={
console.log(data);
});
}
getAPI();
我希望当我输入"minuk\u test.html"时,html执行javascript文件.
但我有两个问题.
首先,我运行我的服务器.然后输入localhost:9999/Project/html/minuk\u test.html
其次,我在服务器打开的情况下运行javascript文件.
Response {
size: 0,
[Symbol(Body internals)]: {
body: PassThrough {
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 5,
_maxListeners: undefined,
_writableState: [WritableState],
allowHalfOpen: true,
[Symbol(kCapture)]: false,
[Symbol(kCallback)]: null
},
stream: PassThrough {
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 5,
_maxListeners: undefined,
_writableState: [WritableState],
allowHalfOpen: true,
[Symbol(kCapture)]: false,
[Symbol(kCallback)]: null
},
boundary: null,
disturbed: false,
error: null
},
type: 'default',
url: 'http://127.0.0.1:9999/store/restaurant_info',
status: 200,
statusText: 'OK',
headers: {
connection: 'close',
'content-length': '8035',
'content-type': 'application/json; charset=utf-8',
date: 'Mon, 30 May 2022 11:53:15 GMT',
etag: 'W/"1f63-IYBGYq/MlY5TBn49Gp7cFQ7aH+A"',
'x-powered-by': 'Express'
},
counter: 0,
highWaterMark: 16384
}
}
undefined //console.log(data);
回答大小为0.这正常吗?
postman 可以接收json数据.
我不明白为什么我的html不能接收json.