Search code examples
javascriptnode.jshttpserver

404 Not found node


Hi so I have been using this node server for some time and it recently stopped working (presumably due to some logical error I mistakenly adjusted), throwing a 404 when I run the server. When I call on it with an http request it throws a 404 as well and shows the same on load from the actual URL in a browser. What is going on?

enter image description here

en[![enter image description hereter 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;

Other details:

Two important files: index.js & api.js but functionality is all in api.js which is why URL stem goes: .../api/...


Solution

  • Ok got the issue and its as silly as it can get. Are you sure you started the right server ? Its definitely not the node server. Its the http-server you have started. To start the server through node you need to go into the directory (in the terminal) and write "node index.js". Necessary code to start a http-server is written inside index.js.

    Got this from the below screenshot.

    enter image description here