로컬 프로젝트 디렉토리 폴더에 로컬로 존재하는 두 개의 JSON 데이터 파일을 요청하는 데 request-promise를 사용하고 있습니다. 예 :NodeJS - '오류 : 잘못된 URI'/ ''
그러나, 나는 500 내부 서버 오류가 발생하고는 뷰에 데이터를 내 노드 콘솔 출력을 통과 할 때 'Error: Invalid URI "/"',
아래를 참조하십시오 :
server.js
let express = require('express');
let app = express();
let path = require('path');
const rp = require("request-promise");
//STORE PATH for local JSON files on variables
let guest = require('./public/data/Companies');
let hotel = require('./public/data/Guests');
app.set("port", process.env.PORT || 5000);
//GET JSON
//Question: Is it okay to pass uri:guest
app.get('/data', function(req, res) {
Promise.all([rp({uri: guest, json: true}), rp({uri: hotel, json: true})]).then(function([hotels, guests]) {
//res.json({hotels, guests});
res.send({hotels, guests});
console.log(hotels, guests);
}).catch(function(err) {
console.log(err);
res.status(500).end();
});
});
//CATCHALL
app.get("/*", function(req,res){
let file = req.params[0] || "/views/index.html";
res.sendFile(path.join(__dirname, "/public/", file));
});
//SET PORT
app.listen(app.get("port"), function(){
console.log("Listening on port: " , app.get("port"));
});
다음 client.js에 :
$(function() {
$.ajax({
type: "GET",
url: "/data",
success: function (res) {
console.log(res);
}
});
});
CLD는 JSON 파일 모두를 얻기 위해 (FS)를 사용하는 예를 게시 개 pls? @ 올리브 리에티 – tonkihonks13