//define a comment schema for the database constCommentSchema = new mongoose.Schema({ creator_name: String, parent: String, // links to the _id of a parent story (_id is an autogenerated field by Mongoose). content: String, });
// compile model from schema module.exports = mongoose.model("comment", CommentSchema);
story.js
1 2 3 4 5 6 7 8 9 10
const mongoose = require("mongoose");
//define a story schema for the database constStorySchema = new mongoose.Schema({ creator_name: String, content: String, });
// compile model from schema module.exports = mongoose.model("story", StorySchema);
/* |-------------------------------------------------------------------------- | server.js -- The core of your server |-------------------------------------------------------------------------- | | This file defines how your server starts up. Think of it as the main() of your server. | At a high level, this file does the following things: | - Connect to the database | - Sets up server middleware (i.e. addons that enable things like json parsing) | - Hooks up all the backend routes specified in api.js | - Fowards frontend routes that should be handled by the React router | - Sets up error handling in case something goes wrong when handling a request | - Actually starts the webserver */
// validator runs some basic checks to make sure you've set everything up correctly // this is a tool provided by staff, so you don't need to worry about it const validator = require("./validator"); validator.checkSetup();
// import libraries needed for the webserver to work! const express = require("express"); // backend framework for our node server. const mongoose = require("mongoose"); const path = require("path"); // provide utilities for working with file and directory paths
const api = require("./api");
// Server configuration below // TODO change connection URL after setting up your own database const mongoConnectionURL = "mongodb+srv://weblab:jAT4po55IAgYWQgR@catbook-ylndp.mongodb.net/test?retryWrites=true&w=majority"; // TODO change database name to the name you chose const databaseName = "catbook"; const options = { useNewUrlParser: true, useUnifiedTopology: true, dbName: databaseName};
// connect to mongodb mongoose .connect(mongoConnectionURL, options) .then(() =>console.log("Connected to MongoDB")) .catch((err) =>console.log(`Error connecting to MongoDB: ${err}`));
// create a new express server const app = express(); app.use(validator.checkRoutes);
// allow us to parse POST request data using middleware app.use(express.json());
// load the compiled react files, which will serve /index.html and /bundle.js const reactPath = path.resolve(__dirname, "..", "client", "dist"); app.use(express.static(reactPath));
// for all other routes, render index.html and let react router handle it app.get("*", (req, res) => { res.sendFile(path.join(reactPath, "index.html")); });
// any server errors cause this function to run app.use((err, req, res, next) => { const status = err.status || 500; if (status === 500) { // 500 means Internal Server Error console.log("The server errored when processing a request!"); console.log(err); }