请求在express中挂起
我有以下特快专递:请求在express中挂起,express,Express,我有以下特快专递: app.post('/template-preview', (req, res) => { console.log(`here is req: ${req}`); res.send('it was hit'); res.render('main', {layout : 'index'}); }); 然后,我使用Axios进行以下调用: const getPreviewTemplate = async () => { console.l
app.post('/template-preview', (req, res) => {
console.log(`here is req: ${req}`);
res.send('it was hit');
res.render('main', {layout : 'index'});
});
然后,我使用Axios进行以下调用:
const getPreviewTemplate = async () => {
console.log('in get previewtemplate')
const result = await axios.post('http://localhost:5000/template-preview',
{firstName: 'frank', lastName: 'gomez'}
);
result.then(res => {
console.log(res);
})
return result;
}
我可以确认以下工作,因为我得到了console.log('in get previewtemplate');
然而,在那之后它就挂了。当我进入chrome中的“网络”选项卡时,我会看到请求的“待定”。就这样
在我的快递代码中,我要在出发前处理cors。不确定这是否导致问题:app.use(cors)代码>
我做错了什么?如果没有正确解析json,这是很常见的。确保在服务器文件中使用
server.use(express.json())
如果您已经有了这个,请确保它位于正确的位置,因为它是自上而下阅读的,所以位置很重要。这里有很多错误。很难确定是哪一个导致了您的问题,因为除了“它挂起”之外,您根本没有向我们提供任何诊断
首先,getPreviewTemplate()
完全没有错误处理。如果您的axios.post()
其次,const result=await axios.post()
返回实际结果,而不是承诺。所以,您不能对其执行result.then()
第三,console.log(\
hereisreq:${req}`)<代码>对您没有帮助,因为它将尝试将req转换为字符串,该字符串可能只是“[Object]”。相反,您需要执行以下操作:
console.log('hereisreq:',req)因此,您将整个对象传递到
console.log()`并让它使用其显示智能来显示对象
第四,app.use(cors)
应该是app.use(cors())这很可能就是为什么每个请求都会被卡住的原因,因为next()
从未被调用过
第五,每个传入请求只需发送一个响应,因此尝试同时执行res.send()
和res.render()
是错误的。选择其中一个,而不是两个
总结如下:
更改此项:
app.use(cors)
app.post('/template-preview', (req, res) => {
console.log(`here is req: ${req}`);
res.send('it was hit');
res.render('main', {layout : 'index'});
});
const getPreviewTemplate = async () => {
console.log('in get previewtemplate')
const result = await axios.post('http://localhost:5000/template-preview',
{firstName: 'frank', lastName: 'gomez'}
);
result.then(res => {
console.log(res);
})
return result;
}
为此:
app.use(cors());
app.post('/template-preview', (req, res) => {
console.log('here is req: ', req);
res.render('main', {layout : 'index'});
});
const getPreviewTemplate = async () => {
console.log('in get previewtemplate')
const result = await axios.post(
'http://localhost:5000/template-preview',
{firstName: 'frank', lastName: 'gomez'}
);
console.log(result);
// this will be the resolved value of the promise
// that this async function returns
return result;
}
更改此项:
app.use(cors)
app.post('/template-preview', (req, res) => {
console.log(`here is req: ${req}`);
res.send('it was hit');
res.render('main', {layout : 'index'});
});
const getPreviewTemplate = async () => {
console.log('in get previewtemplate')
const result = await axios.post('http://localhost:5000/template-preview',
{firstName: 'frank', lastName: 'gomez'}
);
result.then(res => {
console.log(res);
})
return result;
}
为此:
app.use(cors());
app.post('/template-preview', (req, res) => {
console.log('here is req: ', req);
res.render('main', {layout : 'index'});
});
const getPreviewTemplate = async () => {
console.log('in get previewtemplate')
const result = await axios.post(
'http://localhost:5000/template-preview',
{firstName: 'frank', lastName: 'gomez'}
);
console.log(result);
// this will be the resolved value of the promise
// that this async function returns
return result;
}
更改此项:
app.use(cors)
app.post('/template-preview', (req, res) => {
console.log(`here is req: ${req}`);
res.send('it was hit');
res.render('main', {layout : 'index'});
});
const getPreviewTemplate = async () => {
console.log('in get previewtemplate')
const result = await axios.post('http://localhost:5000/template-preview',
{firstName: 'frank', lastName: 'gomez'}
);
result.then(res => {
console.log(res);
})
return result;
}
为此:
app.use(cors());
app.post('/template-preview', (req, res) => {
console.log('here is req: ', req);
res.render('main', {layout : 'index'});
});
const getPreviewTemplate = async () => {
console.log('in get previewtemplate')
const result = await axios.post(
'http://localhost:5000/template-preview',
{firstName: 'frank', lastName: 'gomez'}
);
console.log(result);
// this will be the resolved value of the promise
// that this async function returns
return result;
}
然后,无论谁调用getPreviewTemplate()
都将得到一个承诺,并且必须使用Wait
或。然后()。正如您所说,我添加了。您也不能这样做:console.log(`hereisreq:${req}`)代码>。将其更改为console.log('hereisreq:',req)代码>。而且,不能同时调用res.send()
和res.render()
。每个请求都试图向传入的请求发送一个单独的响应,每个请求只能得到一个响应。