Created
March 15, 2021 05:12
-
-
Save tjkhara/0ca1f7fee3cbc7cd99a9120aa2e48287 to your computer and use it in GitHub Desktop.
mongoose, express boiler plate code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const express = require("express"); | |
| const bodyParser = require("body-parser"); | |
| const ejs = require("ejs"); | |
| const mongoose = require("mongoose"); | |
| const app = express(); | |
| app.set("view engine", "ejs"); | |
| app.use(bodyParser.urlencoded({ extended: true })); | |
| app.use(express.static("public")); | |
| mongoose.connect("mongodb://localhost:27017/wikiDB", { | |
| useNewUrlParser: true, | |
| useUnifiedTopology: true, | |
| }); | |
| // Create the schema | |
| const arcticlesSchema = new mongoose.Schema({ | |
| title: String, | |
| content: String, | |
| }); | |
| // Create the model | |
| const Article = mongoose.model("Article", arcticlesSchema); | |
| // Create an article | |
| // Create three default items | |
| const article1 = new Article({ | |
| title: "First article vis mongoose", | |
| content: | |
| "Scelerisque eleifend donec pretium vulputate sapien. Rhoncus urna neque viverra justo nec ultrices. Arcu dui vivamus arcu felis bibendum. Consectetur adipiscing elit duis tristique. Risus viverra adipiscing at in tellus integer feugiat. Sapien nec sagittis aliquam malesuada bibendum arcu vitae. Consequat interdum varius sit amet mattis. Iaculis nunc sed augue lacus. Interdum posuere lorem ipsum dolor sit amet consectetur adipiscing elit. Pulvinar elementum integer enim neque. Ultrices gravida dictum fusce ut placerat orci nulla. Mauris in aliquam sem fringilla ut morbi tincidunt. Tortor posuere ac ut consequat semper viverra nam libero.", | |
| }); | |
| article1.save(function(err){ | |
| if(err){ | |
| console.log(err) | |
| } | |
| }) | |
| let port = process.env.PORT; | |
| if (port == null || port == "") { | |
| port = 3000; | |
| } | |
| app.listen(port, function () { | |
| console.log("Server has started."); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment