简体   繁体   English

Node.js POST方法错误404

[英]Node.js POST method error 404

I built an index.js file which sends SMS based on body input while posting with Postman. 我建立了一个index.js文件,该文件在通过Postman发布时根据正文输入发送SMS。 The code is working and looks like this (I have hidden my apiKey and apiSecret for this preview) 该代码正在运行,并且看起来像这样(对于此预览,我已经隐藏了我的apiKey和apiSecret)

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const Nexmo = require('nexmo');
const nexmo = new Nexmo({
   apiKey: 'hidden.apiKey',
   apiSecret: 'hidden.apiSecret'
   });

app.post('/send', (req, res) => {
// Sending SMS via Nexmo
nexmo.message.sendSms(
    '4542542445', req.body.toNumber, req.body.message, {type: 'unicode'},
  (err, responseData) => {if (responseData) {console.log(responseData)}}
);
}); 
const server = app.listen(3000);
console.log("starting server")

It woks fine and I receive an SMS message when I run the file, and a post to the route using Postman. 可以正常运行,并且在运行文件时收到一条SMS消息,并使用Postman发送至该路由。

I am trying to implement the same in my bigger project, where I have separate client and server folders representing my frontend and backend. 我试图在更大的项目中实现相同的功能,在该项目中,我有分别代表前端和后端的客户端和服务器文件夹。

When I add the code to my app.js file, I run into Status code 404 not found error. 当我将代码添加到我的app.js文件中时,我遇到状态代码404 not found错误。 Here is the code of my app.js: 这是我的app.js的代码:

const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const morgan = require('morgan')
const {sequelize} = require('./models')
const config = require('./config/config')
const Nexmo = require('nexmo')

const app = express()

app.use(morgan('combined'))

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false}))

app.use(cors())

require('./routes')(app)

sequelize.sync()
.then(() => {
    app.listen(config.port)
    console.log(`Server started on port ${config.port}`)
})

const nexmo = new Nexmo({
apiKey: 'hidden.apiKey',
apiSecret: 'hidden.apiSecret'
}, {debug: true})

app.post('/send', (req, res) => {
// Sending SMS via Nexmo
nexmo.message.sendSms(
    '4542542445', req.body.toNumber, req.body.message, {type: 'unicode'},
  (err, responseData) => {if (responseData) {console.log(responseData)}}
);
  });

I am asking for help to try figure out what is wrong and why it does not hit the route, instead returning status code 404. 我正在寻求帮助,以尝试找出问题所在以及为什么它没有到达路由,而是返回状态代码404。

I can share my github or we can talk on discord: soko#8667 我可以分享我的github,或者我们可以讨论不和:soko#8667

I appreciate your thoughts and help. 感谢您的想法和帮助。

Routes in express should be written : 快递路线应写成:

let route =  require('routes');
app.use('/', route);

I managed to make it work. 我设法使它起作用。

I was adding new route while my server was running. 我正在运行服务器时添加新路由。

I restarted my PC and ran server and client again and it seems work. 我重新启动PC并再次运行服务器和客户端,这似乎可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM