我在 /index.jade
文件中有一个按钮,按下它就会执行
window.location.replace("https://upload-andsize.glitch.me/up")
函数。
它所做的唯一的事情就是将我从 /
重定向到 /up
到目前为止一切顺利。
如果我发出 get 请求,一切正常,但是!... 发布请求将不起作用
app.post("/up",function(req,res){
res.send("hello-world")
})
//will return Cannot GET /up
server code
var express = require('express'),
app = express();
app.set("view engine","jade")
app.use(express.static('public'));
app.get("/",function(req,res,next){
res.render(__dirname+"/views/index.jade",{})
next()
})
app.post("/up",function(req,res){
res.send("hi")
})
var listener = app.listen(process.env.PORT || 3000, function () {
console.log('Your app is listening on port ' + listener.address().port);
});
jade code
html
script(src= "client.js")
title!=title
h1 upload a file and see its weight
input(type="file" id="myfile")
input(type="button" onClick="send()" value="submit")
client code
function send(){
window.location.replace("https://upload-andsize.glitch.me/up")
}
最佳答案
好吧,问题是缺乏对路由如何工作的理解
解决方案是我不发送post请求,而是发送get请求
这就是为什么我收到错误
将html更改为
form(action="/up" method="POST")
input(type="file" id="myfile")
input(type="submit" value="submit")
一切都很顺利,希望它能在未来帮助其他人
关于javascript - app.post 不工作! "Cannot GET/up",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44823890/