我用Express.js构建了一个vanilla Node服务器,只是为了解析一个JSON文件并返回一个数组=&gt; <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="sort_select_delete_form" method="get">
<div class="btn-group">
<label for="check-all"><input type="checkbox" class="btn btn-primary" id="check-all">Select All</label>
<input type="submit" class="btn btn-danger" value="Delete Selected">
</div>
<input class="form-check-input" type="checkbox" value="1000000001" name="deleteid[]">select1
<input class="form-check-input" type="checkbox" value="1000000002" name="deleteid[]">select2
<input class="form-check-input" type="checkbox" value="1000000003" name="deleteid[]">select3
<input class="form-check-input" type="checkbox" value="1000000004" name="deleteid[]">select4
<input class="form-check-input" type="checkbox" value="1000000005" name="deleteid[]">select5
</form>
Vis vis,我在http://localhost:3000
上构建了一个Vue客户端,其中一个axios get请求调用上面的节点服务器地址
http://localhost:8080
const express = require('express');
const app = express();
const port = 3000;
let fs = require('fs');
app.get('/', (request, response) => {
function parseMeGood() {
fs.readFile('./cityList.json', 'utf8', function (err, data) {
if (err) throw err;
let cityArr = [];
let obj = JSON.parse(data);
let city;
for (city of obj) {
if (city.name === 'Huntsville') {
cityArr.push({"country": city.country});
}
}
// console.log(cityArr);
return cityArr;
});
}
response.send(parseMeGood());
});
app.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is listening on ${port}`)
});
200
,它在服务器端运行良好。console.log(cityArr);
),一切都很好。response.send('testing123');
这样的同步函数可以延迟Node的异步性质,而不是“Nodey”,但我也知道这样做没有问题。请纠正我的方式,这样我才能再次坚强。
答案 0 :(得分:0)
app.get('/', (request, response) => {
fs.readFile('./cityList.json', 'utf8', function (err, data) {
if (err) response.status(500).send();
let cityArr = [];
let obj = JSON.parse(data);
let city;
for (city of obj) {
if (city.name === 'Huntsville') {
cityArr.push({"country": city.country});
}
}
response.send(cityArr);
});
});