2017-02-22 102 views
0

注意我沒有洗牌通過很多答案,找不到一個匹配我的答案。快遞 - 不能POST /報價

試圖學習快遞和節點。在index.html中提交表單時,以下內容在瀏覽器中返回「無法POST /引用」,控制檯中沒有任何內容,但GET方法正常工作並加載頁面。

const express = require("express"); 
const app = express(); 

app.listen(3000,() => { 
    console.log("listening to port 3000"); 
}); 

app.get('/', (req, res) => { 
    res.sendFile(__dirname + "/index.html"); 
}); 

app.post('/post', (req, res) => { 
    console.log("Hellloooooooooo!"); 
}); 

指數

<!DOCTYPE html> 

<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta charset="utf-8" /> 
    <link type="text/css" rel="stylesheet" href="stylesheet.css" /> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
    <script src="script.js" type="text/javascript"></script> 
    <title></title> 
</head> 
<body> 
    <form action="/quotes" method="POST"> 
     <input type="text" placeholder="name" name="name"> 
     <input type="text" placeholder="quote" name="quote"> 
     <button type="submit">SUBMIT</button> 
    </form> 
</body> 
</html> 
+0

app.post('/後',(req,res)=> {should change to/quotes –

+0

你沒有爲'/ quotes'定義的路由,只有'/ post'。你的客戶正在試圖發表一篇文章給'/ quotes由...定義你的表單的「動作」屬性 –

回答

1

沒有引號的路線,你只有2路,只有一個職位/post路線,但無後/quotes路線

const express = require("express"); 
const app = express(); 

app.listen(3000,() => { 
    console.log("listening to port 3000"); 
}); 

app.get('/', (req, res) => { 
    res.sendFile(__dirname + "/index.html"); 
}); 

app.post('/post', (req, res) => { 
    console.log("Hellloooooooooo!"); 
}); 

// just make a simple quotes route 

app.post('/quotes', (req, res) => { 
    console.log("hello from quotes route!"); 
}); 
+0

就是這樣!謝謝! – jylny

+0

通過標記來接受答案,這也會幫助他人 –

+0

正在等待時間限制^。^ – jylny