Express中的res.redirect有问题
Express中的res.redirect有问题
我一直在尝试从搜索表单中输入我的位置,它会登录到控制台,但是res.redirect不会将我带到新的URL。 (给出的URL仅是示例)
const express = require('express');
const cors = require('cors');
// Utils
const geocode = require('./utils/geocode');
const forecast = require('./utils/weatherstack');
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cors())
let location;
app.post('/search-location', async (req,res) => {
try {
location = await req.body.location
return res.redirect('')
} catch (e) {
res.status(500).send(e)
}
})
app.listen(9000, () => {
console.log("server is up and running on 9000");
})
回答如下:我精简了您的代码,以下代码段为我提供了正确的重定向:
const express = require('express');
const app = express();
app.post('/search-location', async (req,res) => {
try {
return res.redirect('https://www.google')
} catch (e) {
res.status(500).send(e)
}
})
app.listen(9000, () => {
console.log("server is up and running on 9000");
})
curl -X POST localhost:9000/search-location
* Trying ::1:9000...
* Connected to localhost (::1) port 9000 (#0)
> POST /search-location HTTP/1.1
> Host: localhost:9000
> User-Agent: curl/7.69.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 302 Found
< X-Powered-By: Express
< Location: https://www.google
< Vary: Accept
< Content-Type: text/plain; charset=utf-8
< Content-Length: 44
< Date: Thu, 07 May 2020 16:37:00 GMT
< Connection: keep-alive
<
* Connection #0 to host localhost left intact
Found. Redirecting to https://www.google
您的问题很可能是location = await req.body.location
,因为req.body.location
不是一个承诺(只是客户端解析的POST正文对象,无法等待它。
也许您打算将其包装在函数中:
location = await computeFunction(req.body.location)
最新文章
- 程序员的情感修养 —— 专访“非诚勿扰”牵手成功男嘉宾程序员石川
- linux 下dump的使用
- MinGW+MSYS安装
- Socket网络编程详解
- 职场经验
- 微软上海招 Principal Manager!
- Service Principal 介绍
- 倾听:不只是听见
- JMeter BeanShell 应用
- Ext 2 概述
- calendar java 线程安全
- SAR成像系列:【11】干涉合成孔径雷达(干涉SAR,Interferometric SAR,InSAR)
- Hashtable的原理
- linux终端基本操作命令
- mysql upsert语法
- SSL单向认证和双向认证交互流程
- HTTPS 中双向认证SSL 协议的具体过程
- 基于STM32F429的语音识别交互系统
- jdbcDAO
- 计算机软件可以用虚拟串口吗,虚拟串口以及在Proteus中的使用