Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
请求在express中挂起_Express - Fatal编程技术网

请求在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()
。每个请求都试图向传入的请求发送一个单独的响应,每个请求只能得到一个响应。