Невозможно получить значение и получить ответ в приложении узла - PullRequest
0 голосов
/ 10 июля 2020

Я создал приложение на Node и с помощью MongoDB Atlas. Я мог подключить приложение к БД. Я мог получать сообщение «Подключено к БД» и видеть коллекции, созданные в кластере. Но когда я пытаюсь запросить эту коллекцию, приложение вылетает. Маршруты работают нормально. Я не мог понять, в чем проблема.

приложение. js выглядит следующим образом:

const express = require("express");
const morgan = require("morgan");
const app = express();
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const session = require("express-session");
const mongoose = require("mongoose");
const { MongoClient } = require("mongodb");
require('dotenv').config();

const facultyRoutes = require("./api/routes/faculty");
const userRoutes = require("./api/routes/user");
const adminRoutes = require("./api/routes/admin");

//DB Connectivity
const uri =
  "mongodb+srv://Shreyas:"+process.env.SECRET_KEY+"@cluster0.dntfc.mongodb.net/Exam?retryWrites=true&w=majority";
/*const client = new MongoClient(uri);
client
  .connect({
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useFindAndModify: false,
    useCreateIndex: true,
  })
  .then((result) => console.log("Connected to DB"))
  .catch((error) => console.log(error));*/

  mongoose
  .connect(uri, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useFindAndModify: false,
    useCreateIndex: true,
  })
  .then((result) => console.log("Connected to DB"))
  .catch((error) => console.log(error));

/*mongoose
  .connect("mongodb://localhost:27017/Exam", {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useFindAndModify: false,
    useCreateIndex: true,
  })
  .then((result) => console.log("Connected to DB"))
  .catch((error) => console.log(error));*/

mongoose.Promise = global.Promise;

//middlewares
app.use("/uploads", express.static("uploads"));
app.set("view engine", "ejs");
app.use(morgan("combined"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(
  session({
    secret: "sdfghj",
    resave: false,
    saveUninitialized: true,
    cookie: {
      maxAge: 3600000,
      httpOnly: true,
    },
  })
);

app.use((error, req, res, next) => {
  res.status(error.status || 500);
  res.header("Access-Control-Allow-Origin", "*");
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept, Authorization"
  );

  if (req.method === "OPTIONS") {
    res.header("Access-Control-Allow-Headers", "PUT, POST, GET");
    return res.status(200).json({});
  }

  next();
});

// Routes which should handle requests
app.use("/user", userRoutes);
app.use("/faculty", facultyRoutes);
app.use("/admin", adminRoutes);
app.get("/", (req, res) => {
  res.render("index");
});

app.get("/user/userRegister", (req, res) => {
  res.render("register");
});

app.get("/admin/adminLogin", (req, res) => {
  res.render("./admin/admin");
});

app.use((req, res, next) => {
  const error = new Error("Not found");
  error.status = 404;
  next(error);
});

app.use((error, req, res, next) => {
  res.status(error.status || 500);
  res.json({
    error: {
      message: error.message,
    },
  });
});

module.exports = app;

Ошибка:

2020-07-10T10:52:50.008084+00:00 app[web.1]: > online_test_backend@1.0.0 start /app
2020-07-10T10:52:50.008085+00:00 app[web.1]: > node server.js
2020-07-10T10:52:50.008085+00:00 app[web.1]: 
2020-07-10T10:52:50.955153+00:00 app[web.1]: Warning: connect.session() MemoryStore is not
2020-07-10T10:52:50.955211+00:00 app[web.1]: designed for a production environment, as it will leak
2020-07-10T10:52:50.955211+00:00 app[web.1]: memory, and will not scale past a single process.
2020-07-10T10:52:50.959096+00:00 app[web.1]: Server is listening to 56970
2020-07-10T10:52:51.554456+00:00 heroku[web.1]: State changed from starting to up
2020-07-10T10:52:52.322444+00:00 app[web.1]: (node:23) DeprecationWarning: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead.
2020-07-10T10:52:52.322539+00:00 app[web.1]: Connected to DB
2020-07-10T10:53:43.130535+00:00 app[web.1]: 
2020-07-10T10:53:43.130544+00:00 app[web.1]: <--- Last few GCs --->
2020-07-10T10:53:43.130547+00:00 app[web.1]: ai[23:0x386f160]    52403 ms: Mark-sweep 255.5 (257.6) -> 255.2 (257.8) MB, 335.1 / 0.0 ms  (+ 0.0 ms in 1 steps since start of marking, biggest step 0.0 ms, walltime since start of marking 336 ms) (average mu = 0.125, current mu = 0.003) finalize increment[23:0x386f160]    52714 ms: Mark-sweep 256.2 (257.8) -> 255.9 (257.8) MB, 163.9 / 0.0 ms  (+ 109.4 ms in 163 steps since start of marking, biggest step 14.1 ms, walltime since start of marking 311 ms) (average mu = 0.123, current mu = 0.121) allocation fa
2020-07-10T10:53:43.130547+00:00 app[web.1]: 
2020-07-10T10:53:43.130548+00:00 app[web.1]: <--- JS stacktrace --->
2020-07-10T10:53:43.130548+00:00 app[web.1]: 
2020-07-10T10:53:43.130550+00:00 app[web.1]: FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
2020-07-10T10:53:43.134116+00:00 app[web.1]:  1: 0xa0bb60 node::Abort() [node]
2020-07-10T10:53:43.134642+00:00 app[web.1]:  2: 0xa0bf6c node::OnFatalError(char const*, char const*) [node]
2020-07-10T10:53:43.135176+00:00 app[web.1]:  3: 0xb820fe v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
2020-07-10T10:53:43.135693+00:00 app[web.1]:  4: 0xb82479 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
2020-07-10T10:53:43.136225+00:00 app[web.1]:  5: 0xd2ee35  [node]
2020-07-10T10:53:43.136857+00:00 app[web.1]:  6: 0xd2f4c6 v8::internal::Heap::RecomputeLimits(v8::internal::GarbageCollector) [node]
2020-07-10T10:53:43.137427+00:00 app[web.1]:  7: 0xd3bd45 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [node]
2020-07-10T10:53:43.137965+00:00 app[web.1]:  8: 0xd3cbf5 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
2020-07-10T10:53:43.138582+00:00 app[web.1]:  9: 0xd3d40a v8::internal::Heap::FinalizeIncrementalMarkingIfComplete(v8::internal::GarbageCollectionReason) [node]
2020-07-10T10:53:43.139134+00:00 app[web.1]: 10: 0xd40cbf v8::internal::IncrementalMarkingJob::Task::RunInternal() [node]
2020-07-10T10:53:43.139664+00:00 app[web.1]: 11: 0xc81a83 v8::internal::CancelableTask::Run() [node]
2020-07-10T10:53:43.140173+00:00 app[web.1]: 12: 0xa7a924 node::PerIsolatePlatformData::RunForegroundTask(std::unique_ptr<v8::Task, std::default_delete<v8::Task> >) [node]
2020-07-10T10:53:43.140667+00:00 app[web.1]: 13: 0xa7b3a5 node::PerIsolatePlatformData::FlushForegroundTasksInternal() [node]
2020-07-10T10:53:43.141405+00:00 app[web.1]: 14: 0x13333ee  [node]
2020-07-10T10:53:43.142121+00:00 app[web.1]: 15: 0x1346008  [node]
2020-07-10T10:53:43.142794+00:00 app[web.1]: 16: 0x1333c2f uv_run [node]
2020-07-10T10:53:43.143244+00:00 app[web.1]: 17: 0xa4ecf5 node::NodeMainInstance::Run() [node]
2020-07-10T10:53:43.143681+00:00 app[web.1]: 18: 0x9dcc68 node::Start(int, char**) [node]
2020-07-10T10:53:43.143709+00:00 app[web.1]: 19: 0x7ff8e638cb97 __libc_start_main [/lib/x86_64-linux-gnu/libc.so.6]
2020-07-10T10:53:43.144185+00:00 app[web.1]: 20: 0x979215  [node]
2020-07-10T10:53:43.172641+00:00 app[web.1]: Aborted
2020-07-10T10:53:43.175555+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-07-10T10:53:43.175845+00:00 app[web.1]: npm ERR! errno 134
2020-07-10T10:53:43.176858+00:00 app[web.1]: npm ERR! online_test_backend@1.0.0 start: `node server.js`
2020-07-10T10:53:43.177006+00:00 app[web.1]: npm ERR! Exit status 134
2020-07-10T10:53:43.177176+00:00 app[web.1]: npm ERR! 
2020-07-10T10:53:43.177367+00:00 app[web.1]: npm ERR! Failed at the online_test_backend@1.0.0 start script.
2020-07-10T10:53:43.177503+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-07-10T10:53:43.187317+00:00 app[web.1]: 
2020-07-10T10:53:43.187567+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-07-10T10:53:43.187701+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-07-10T10_53_43_178Z-debug.log
2020-07-10T10:53:43.254943+00:00 heroku[web.1]: Process exited with status 134
2020-07-10T10:53:43.291156+00:00 heroku[web.1]: State changed from up to crashed

Когда я вызываю любой маршрут sh.

Спасибо.

...