Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

Localhost:3000 not opening from node.js app

I've set up a tiny little app which is run using Express, Nodemon and Mongoose.

A few hours ago, it worked fine - and I don't think I've changed any of the code then. If I have, it must have been accidentally.

But whenever I try to access my localhost for this particular app, it doesn't load. It just sits in this state of loading. No errors appear, the console is clear and it states 'Running on Port 3000'.

If I try other apps, they work fine on localhost, so it must be the code - but I don't even know where to start considering there's no error messages.

Here's my code:

const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
var encrypt = require('mongoose-encryption');

require("dotenv").config();

const app = express();

mongoose.connect(process.env.MONGO_URI, {useNewUrlParser: true, useUnifiedTopology: true});

  const SubmitDebtSchema = new Schema ({
    balance: [{
      balanceDate: Date,
      newBalance: Number
    }],
    monthly: Number,
    date: String,
    autoBalance: String
  });

  const encKey = process.env.ENCKEY;
  const sigKey = process.env.SIGKEY;

  SubmitDebtSchema.plugin(encrypt, { encryptionKey: encKey, signingKey: sigKey, excludeFromEncryption: ['autoBalance'] });

  const SubmitDebt = mongoose.model('submitdebt', SubmitDebtSchema);

  app.get("/", async (req, res) => {

  const debts = await SubmitDebt.find({ autoBalance: true });

  debts.map(debt => {

    console.log(debt.id);

  const debtDate = parseInt(debt.date);
  const finalDate = new Date(new Date().setDate(debtDate));

  console.log(finalDate);

  const todaysDate = new Date();

  console.log(todaysDate);

  const debtBalance = debt.balance[debt.balance.length - 1].newBalance;

  console.log(debtBalance);

  const debtRepayment = debt.monthly;

  console.log(debtRepayment);

  const updatedBalance = { balanceDate: new Date(), newBalance: debtBalance - debtRepayment };
  console.log(updatedBalance);

  if (todaysDate < finalDate) {
    console.log("Balance shouldn't change.");
  }

  if (todaysDate >= finalDate) {
    console.log("Balance should change");
    SubmitDebt.findById(debt._id, (err, submitdebt) => {
      if (!submitdebt) {
        console.log("Unable to find entry to edit.");
      }
      else {
        submitdebt.balance.push(updatedBalance);

        submitdebt.save().then(submitdebt => {
          console.log("Debt successfully updated.");
        })
        .catch(err => {
          console.log("Debt unsuccessfully updated.");
        });
      }
    });
  }


  res.send(debts);

  });

});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
    console.log(`Lemio is running on port ${ PORT }`);
});

Can anyone spot any reason why my localhost isn't working for this app? Feel like I'm missing something obvious!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Some thoughts:

  1. kill the process and start it over;
  2. check if your port is busy with some other app;
  3. check is mongod is running.

Links:

How To Use ps, kill, and nice to Manage Processes in Linux

How to check if port is in use on Linux or Unix

Manage mongod Processes


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...