var createError = require('http-errors');
var express = require('express');
var path = require('path');
var logger = require('morgan');
var session = require("express-session");

var cors = require("cors");

var MONGO_HOST = "mongo";
// var MONGO_HOST = "127.0.0.1";

// use assignment2
var monk = require("monk");
var db = monk(`${MONGO_HOST}:27017/assignment2`);

var notesRouter = require("./routes/notes");

var app = express();
app.use(
  cors({
    origin: "http://localhost:3000",
    credentials: true,
    optionsSuccessStatus: 200,
  })
);

// config express-session
app.use(
  session({
    secret: "keyboard cat",
    cookie: { userId: [] },
  })
);

app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, "public")));

// Make our db accessible to routers
app.use(function (req, res, next) {
  req.db = db;
  next();
});

app.options("*", cors());
app.use("/", notesRouter);

// for requests not matching the above routes, create 404 error and forward to error handler
app.use(function(req, res, next) {
    next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development environment
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};

    // render the error page
    res.status(err.status || 500);
    res.render('error');
});

// module.exports = app;
app.listen(3001);
