如何正確配置我的文件?無法連接到後端,我的路徑/文件未找到
我想提交一個表單,當我提交它時,我得到一個404錯誤,我的路徑/文件未找到。
確切的錯誤是:
xhr.js:175 POST http://localhost:1337/mail 404 (Not Found)
我app.js文件是:
var express = require('express');
var mail = require('./routes/mail');
var app = express();
app.use('/mail', mail);
app.use(express.static('dist'));
app.use(express.static('dist/img'));
app.listen(process.env.PORT || 1337);
console.log('Port 1337 is listening');
我在mail.js文件是:
var express = require('express');
var mail = express.Router();
mail.get('/:name/:email/:phone:/message', function(req, res){
console.log('in function');
});
console.log('Made it to mail');
module.exports = mail;
發送代碼的功能從一個jsx文件是:
handleClick(e){
const name = this.state.name;
const email = this.state.email;
const phone = this.state.phone;
const message = this.state.message;
e.preventDefault();
if(name === "" | email === "" | phone === "" | message === ""){
return;
}else{
axios({
method: 'post',
url: '/mail',
data: {name, email, phone, message}
});
}
}
標題顯示以下內容:
Request URL:http://localhost:1337/mail
Request Method:POST
Status Code:404 Not Found
Remote Address:[::1]:1337
Response Headers
view source
Connection:keep-alive
Content-Length:18
Content-Type:text/html; charset=utf-8
Date:Mon, 23 Jan 2017 21:17:15 GMT
X-Content-Type-Options:nosniff
X-Powered-By:Express
Request Headers
view source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:85
Content-Type:application/json;charset=UTF-8
Host:localhost:1337
Origin:http://localhost:1337
Referer:http://localhost:1337/
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
Request Payload
view source
{name: "Username", email: "[email protected]", phone: "1234567891", message: "testing"}
當我運行一飲而盡,我得到的控制檯消息,它「做了它對郵件」。但是當我提交表單時,它給了我錯誤「POST http://localhost:1337/mail 404(Not Found)」。
我的目錄設置爲:
root/
...dist/ (this is where my client.min.js file is where all my front end code is)
...routes/
app.js
我很猶豫說「非常感謝你」,因爲我不知道它是否適合禮儀堆棧溢出,但我必須。感謝您花時間向我解釋這一點,並讓我走上正確的道路。對此,我真的非常感激。 – Mike
我很確定你可以在評論中說*謝謝* :)我很高興我可以幫你。 – Molda