Skip to content

Latest commit

 

History

History
81 lines (65 loc) · 2.11 KB

README.md

File metadata and controls

81 lines (65 loc) · 2.11 KB
Quiz App
Using Node/Express/Mongoose and Docker

Start the App

  1. Please run yarn install to install all the dependencies first
  2. Please run node models/InitialDB.js to initialize data to database
  3. Please run yarn start to start the server

Issues Need to be Fixed

  1. Create new question answers in the array of objects format.
  2. Edit questions with answers in the array of objects format.

Boilerplate Code

npm install express
npm install mongodb
npm install mongoose

const express = require("express");
const mongoose = require("mongoose");
const app = express();

mongoose
  .connect("mongodb://127.0.0.1:27017/mydb", { useNewUrlParser: true })
  .then(() => console.log("MongoDB Connected"))
  .catch((err) => console.log(err));

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

Body Parser Issue

  • You must make sure that you define all configurations BEFORE defining routes.
  • app.use(express.bodyParser()) before app.use(app.router).
  • In fact, app.use(app.router) should be the last thing you call.
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const cors = require('cors');
const path = require("path");
const routes = require('./routes/routes');
const app = express();

require("dotenv").config();

app.set("view engine", "ejs");
app.set('views', path.join(__dirname, '/views')); 

app.use(
  bodyParser.urlencoded({
    extended: true,
  })
);

app.use(cors());  //providing a Connect/Express middleware that can be used to enable CORS with various options.
app.use(express.json());
app.use(routes);

app.get("/", function (req, res) {
  res.render("home");
});  

app.all("*", (req, res) => {
  res.status(404).send("Page not found");
});

mongoose
  .connect("mongodb://127.0.0.1:27017/quiz", { useNewUrlParser: true })
  .then(() => console.log("MongoDB Connected"))
  .catch((err) => console.log(err));

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