404找不到节点

时间:2018-03-13 13:27:05

标签: javascript node.js httpserver

嗨,所以我一直在使用这个节点服务器,它最近停止工作(可能是由于我错误地调整了一些逻辑错误),当我运行服务器时抛出404。当我使用http请求调用它时,它也会抛出404并在浏览器中从实际URL加载时显示相同的内容。发生了什么事?

enter image description here

en[![enter image description here此处的图片说明] 3] 3

index.js:

//Environment Vars
var uri = process.env.NODE_ENV || "development"
console.log(uri + " environment")

//Express App
var express = require('express');
var app = express();

//Api for reading http post request body in express
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json())

//Log Connections
app.use(function timeLog (req, res, next) {
  console.log('incoming connection . . . ')
  console.log(req)
  next()
})

//API middelware
var api = require('./api')
app.use('/api', api)

app.get('/', function (req, res) {
  res.status(200).send(JSON.stringify({ message: 'Welcome!', success: true, error: null }));
}); 

//Create Server
var port = process.env.PORT || 1337;
var httpServer = require('http').createServer(app);
httpServer.listen(port, function () {
  console.log('Server running on port ' + port + '.');
});

api.js

var express = require('express')
var router = express.Router()

var stripe_key = process.env.STRIPE_KEY || "sk_test_P9XQtrrfGYU9mF8h6C47bgUp"
var stripe = require('stripe')(stripe_key);
var request = require("request-promise-native")

//API
router.get('/', function (req, res) {
  res.status(200).send(JSON.stringify({ message: 'API Gateway', success: true, error: null }));
}) // Just for testing, just for error-handling

//1. Create a customer account
router.post('/new_customer', function (req, res) {
  console.log("Creating new customer account...")
  var body = req.body

  stripe.customers.create({ email: body.email, })
    .then((customer) => {
      console.log(customer)
      // Send customerId -> Save this on Firebase for later use
      res.status(200).send(JSON.stringify({ success: true, error: null, customerId: customer.id }));
    })
    .catch((err) => {  
      console.log(err)
      res.status(400).send(JSON.stringify({ success: false, error: err }))
    });
})

//2. Save Credit Card with token
router.post('/new_card', function (req, res) {
  var customerId = req.body.customerId
  var token = req.body.token

  stripe.customers.update(customerId, { source: token })
    .then((customer) => {
      console.log(customer)
      res.status(200).send(JSON.stringify({ success: true, error: null }));
    })
    .catch((err) => {  
      console.log(err)
      res.status(400).send(JSON.stringify({ success: false, error: err }))
    });

})

//3. Use customerId to post a charge
router.post('/new_charge', function (req, res) {
  var customerId = req.body.customerId
  var amount = req.body.amount
  var source = req.body.source
  stripe.charges.create({
    amount: amount,           //in cents
    currency: "usd",
    customer: customerId,      //CUSTOMER_STRIPE_ACCOUNT_ID
    source: source, // obtained with Stripe.js
  }).then((charge) => {
    res.status(200).send(JSON.stringify({ message: 'Sucess.', success: true, error: null }));
  }).catch((error) =>{
    res.status(400).send(JSON.stringify({ message: 'Error', success: false, error: error }));
  })
})


router.post('/ephemeral_keys', (req, res) => {
  const stripe_version = req.body.api_version;
  var customerId = req.body.customerId;

  if (!stripe_version) {
    res.status(400).end();
    return;
  }
  console.log(stripe_version)
  // This function assumes that some previous middleware has determined the
  // correct customerId for the session and saved it on the request object.
  stripe.ephemeralKeys.create(
    {customer: customerId},
    {stripe_version: stripe_version}
  ).then((key) => {

    console.log("Ephemeral key: " + key)
    res.status(200).json(key);
    res.status(200).send(JSON.stringify({ message: 'AAAAhh', success: true, error: null }));
  }).catch((err) => {
    console.log("Ephemeral key error: " + err)
    res.status(200).send(JSON.stringify({ message: 'ABBBBBB', success: true, error: null }));
    res.status(500).end();
  });
});

module.exports = router;

其他细节:

两个重要文件:index.js& api.js但功能都在api.js中,这就是为什么URL干:... / api /...

4 个答案:

答案 0 :(得分:3)

好的问题和它可以得到的愚蠢。你确定你启动了正确的服务器吗?它绝对不是节点服务器。它是你启动的http服务器。要通过节点启动服务器,您需要进入目录(在终端中)并编写" node index.js"。启动http服务器的必要代码写在index.js。

从下面的屏幕截图中了解到这一点。

enter image description here

答案 1 :(得分:1)

我的错误是您在需要GET请求的路由上发出POST请求:

router.post('/new_charge', function (req, res) {
  ...
})

因此,您应检查您是否正在向此路线发出请求,而不是GET请求。你是如何从客户端访问该路线的?

答案 2 :(得分:1)

' / api / new_charge'没有router.get路由,但是有一个router.post路由。

答案 3 :(得分:1)

您的router.get('/new_charge')路线在哪里?它不在您发布的文件中。您可能已将其删除,或者/new_charge路由需要用作帖子而不是get。