mirror of
https://github.com/rjNemo/MERN_sample_app
synced 2026-06-12 03:26:40 +00:00
creates react + redux frontend and express + mongodb backend
This commit is contained in:
commit
5cca4d3a2a
9 changed files with 3533 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
**/node_modules
|
||||||
37
README.md
Normal file
37
README.md
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
# MERN Stack Demo
|
||||||
|
|
||||||
|
## Prequisites
|
||||||
|
|
||||||
|
- NodeJs
|
||||||
|
- npm
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
```
|
||||||
|
npm init -y
|
||||||
|
```
|
||||||
|
|
||||||
|
install dependencies
|
||||||
|
|
||||||
|
```
|
||||||
|
npm i express mongoose concurrently
|
||||||
|
```
|
||||||
|
|
||||||
|
install nodemon for server hot reload and jest for testing
|
||||||
|
|
||||||
|
```
|
||||||
|
npm i -D nodemon jest
|
||||||
|
```
|
||||||
|
|
||||||
|
## Create npm scripts
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"scripts": {
|
||||||
|
"start": "node server.js",
|
||||||
|
"server": "nodemon server.js",
|
||||||
|
"test": "jest",
|
||||||
|
"test:watch": "npm run test -- --watch"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
1
client
Submodule
1
client
Submodule
|
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit cf121255061f45227b17654c5b4e309c2e2a9d1d
|
||||||
3
config/keys.js
Normal file
3
config/keys.js
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
// connection string for mongoDB Atlas
|
||||||
|
export const MONGO_URI =
|
||||||
|
"mongodb+srv://ruidy:Xyxoo971+mongodb@projectscluster-xcfet.mongodb.net/test?retryWrites=true&w=majority";
|
||||||
15
models/Item.js
Normal file
15
models/Item.js
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
import mongoose from "mongoose";
|
||||||
|
|
||||||
|
// create a schema
|
||||||
|
const ItemSchema = new mongoose.Schema({
|
||||||
|
name: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
date: {
|
||||||
|
type: Date,
|
||||||
|
default: Date.now,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export const Item = mongoose.model("item", ItemSchema);
|
||||||
3368
package-lock.json
generated
Normal file
3368
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
38
package.json
Normal file
38
package.json
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
{
|
||||||
|
"name": "mern_demo",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "server.js",
|
||||||
|
"scripts": {
|
||||||
|
"start": "node server.js",
|
||||||
|
"server": "nodemon server.js",
|
||||||
|
"client": "npm start --prefix client",
|
||||||
|
"client-install": "npm install --prefix client",
|
||||||
|
"dev": "concurrently \"npm run server\" \"npm run client\"",
|
||||||
|
"test": "jest",
|
||||||
|
"test:watch": "npm run test -- --watch"
|
||||||
|
},
|
||||||
|
"coverage": "jest --coverage",
|
||||||
|
"keywords": [],
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"@babel/core": "^7.9.0",
|
||||||
|
"@babel/preset-env": "^7.9.5",
|
||||||
|
"body-parser": "^1.19.0",
|
||||||
|
"concurrently": "^5.1.0",
|
||||||
|
"express": "^4.17.1",
|
||||||
|
"moment": "^2.24.0",
|
||||||
|
"mongoose": "^5.9.10",
|
||||||
|
"reactstrap": "^8.4.1"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"nodemon": "^2.0.3"
|
||||||
|
},
|
||||||
|
"type": "module",
|
||||||
|
"babel": {
|
||||||
|
"presets": [
|
||||||
|
"@babel/env"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
40
routes/api/items.js
Normal file
40
routes/api/items.js
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
import express from "express";
|
||||||
|
import { Item } from "../../models/Item.js";
|
||||||
|
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @route GET api/items
|
||||||
|
* @access Public
|
||||||
|
* @description Get all items.
|
||||||
|
*/
|
||||||
|
router.get("/", (req, res) => {
|
||||||
|
Item.find()
|
||||||
|
.sort({ date: -1 })
|
||||||
|
.then((items) => res.json(items));
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @route POST api/items
|
||||||
|
* @access Public
|
||||||
|
* @description Create an item.
|
||||||
|
*/
|
||||||
|
router.post("/", (req, res) => {
|
||||||
|
const newItem = new Item({
|
||||||
|
name: req.body.name,
|
||||||
|
});
|
||||||
|
newItem.save().then((item) => res.json(item));
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @route DELETE api/items/:id
|
||||||
|
* @access Public
|
||||||
|
* @description Delete an item.
|
||||||
|
*/
|
||||||
|
router.delete("/:id", (req, res) => {
|
||||||
|
Item.findById(req.params.id)
|
||||||
|
.then((item) => item.remove().then(() => res.json({ success: true })))
|
||||||
|
.catch((e) => res.status(404).json({ status: "Not Found" }));
|
||||||
|
});
|
||||||
|
|
||||||
|
export default router;
|
||||||
30
server.js
Normal file
30
server.js
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
import express from "express";
|
||||||
|
import mongoose from "mongoose";
|
||||||
|
import moment from "moment";
|
||||||
|
import { MONGO_URI } from "./config/keys.js";
|
||||||
|
import items from "./routes/api/items.js";
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
|
||||||
|
// http port
|
||||||
|
const PORT = process.env.PORT || 5000;
|
||||||
|
// database connection key
|
||||||
|
const db = MONGO_URI;
|
||||||
|
|
||||||
|
// connection to database
|
||||||
|
mongoose
|
||||||
|
.connect(db, { useNewUrlParser: true, useUnifiedTopology: true })
|
||||||
|
.then(() => console.log(`${moment().format()}: MongoDB connected`))
|
||||||
|
.catch((e) => console.error(e));
|
||||||
|
|
||||||
|
// bodyparser middleware
|
||||||
|
app.use(express.json());
|
||||||
|
// Register routes
|
||||||
|
app.use("/api/items/", items);
|
||||||
|
|
||||||
|
// Starts server
|
||||||
|
app.listen(PORT, () =>
|
||||||
|
console.log(
|
||||||
|
`${moment().format()}: Server running on http://localhost:${PORT}`
|
||||||
|
)
|
||||||
|
);
|
||||||
Loading…
Reference in a new issue