Skip to content

Commit

Permalink
add full mern
Browse files Browse the repository at this point in the history
  • Loading branch information
MoralesJonathan committed Sep 6, 2019
1 parent 7582a14 commit 99f4387
Show file tree
Hide file tree
Showing 29 changed files with 17,450 additions and 23 deletions.
Binary file added .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ deploy:
provider: heroku
api_key:
secure: R/+byiFerJ4Wz9OrycAmzSLjnLuY1b08wyC5uS5oiuNcruPX3VjYtSJsq06hQ3BGAylVPyZ0BRrbSPKHR66TR4wJ2Zct2O/cak54kv1Bqp3WzKL4chL2W1nDguHx51qrtJtvZd6Nfki3sIeHCN3ZttgmEsmAgh+JjnImwQZg1QELpVDzr21benJx9ecdwfQjmvqEK5voKUJxsWNt3F0ZgWySlZDUHtYotW5nSTKBW0qsVxlt7OQl08t8m25ZZPykLJ1uFcTSEI5UBnXab4VJ3JPm+xSrSSVs/HrOrRgmOIc9WpIcIE6wj6wJKcgUzU/K3hQOmJiN3X3/8ufPIVLZecJpcdWenhULf+wJEnw3QUHToZO40GHNJarmL4PBnmnhWgBgBxlasmquXp6Z42+7t2j2yLr+OoBZZ92GBdMvvTH+0SRV7C1s3wL9htJcDwC2QaskkJEJt6fJFBmqI2XxxyWUizpIt4ZtMTATCRoRpZRIsNFEJZl0U4tjuuAJ5VmUI3iZDkGI7Krc8oGPeVPNRucX7OhX2ZwbjvqQcvpAe4TMZeAeTz9YoYfFaaFZlx9HK2RxOioLNtNrtcqxVF+0swQUPjJFAUP7yCmX+S8K9blciHu3nkJq9iLos/xkDmrqlLVOd/km4x38h7aRkSRHbgafUqbpU9eH8QmauN+AuVI=
app: emerge2019
app: merntemplate
16,692 changes: 16,692 additions & 0 deletions client/package-lock.json

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "reacttemplate",
"version": "0.1.0",
"private": true,
"proxy": "http://localhost:3001/",
"dependencies": {
"react": "^16.6.3",
"react-dom": "^16.6.3",
"react-scripts": "^2.1.1",
"react-router-dom": "^4.3.1",
"axios": "^0.18.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions controllers/testController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const db = require("../models");

module.exports = {
findAll: function(req, res) {
db.Test
.find(req.query)
.sort({ date: -1 })
.then(dbModel => res.json(dbModel))
.catch(err => res.status(422).json(err));
},
};
25 changes: 25 additions & 0 deletions dbconfig/connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const mongoose = require('mongoose');
require("dotenv").config();

const handleError = e => {
console.error(`Mongoose connection error: ${JSON.stringify(e)}`);
}

const options = {
useNewUrlParser: true,
autoReconnect: true
}

const connect = (URI) =>
mongoose.connect(URI || process.env.MONGODB_URI, options)
.catch(handleError);

mongoose.connection.on('error', handleError);
mongoose.connection.on('connecting', () => console.info('Starting to make initial connection to the MongoDB server...'));
mongoose.connection.on('connected', () => console.info('Mongoose successfully connected to MongoDB server.'));
mongoose.connection.on('disconnecting', () => console.warn('Mongoose is disconnecting from the MongoDB server...'));
mongoose.connection.on('disconnected', () => console.warn('Mongoose lost connection to the MongoDB server. This may be due to an expected connection close, the database server crashing, or network connectivity issues.'));
mongoose.connection.on('reconnected', () => console.info('Mongoose successfully reconnected to MongoDB server.'));
mongoose.connection.on('reconnectFailed', () => console.error('Mongoose was unable to successfully reconnect to MongoDB server. No further attempts will be made.'));

module.exports = connect;
3 changes: 3 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
Test: require("./test")
};
11 changes: 11 additions & 0 deletions models/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const testSchema = new Schema({
title: { type: String, required: true },
date: { type: Date, default: Date.now }
});

const Test = mongoose.model("Test", testSchema);

module.exports = Test;
28 changes: 19 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"name": "reacttemplate",
"name": "merntemplate",
"version": "0.1.0",
"private": true,
"main": "server.js",
"dependencies": {
"if-env": "^1.0.4",
"mongoose": "^5.6.13",
"normalize.css": "^8.0.1",
"react": "^16.9.0",
"react-dom": "^16.9.0",
Expand All @@ -13,20 +16,27 @@
},
"devDependencies": {
"@babel/core": "^7.5.5",
"@storybook/react": "^5.1.10",
"@storybook/addons": "^5.1.10",
"@storybook/addon-a11y": "^5.1.10",
"@storybook/addon-actions": "^5.1.10",
"@storybook/addon-knobs": "^5.1.10",
"@storybook/addon-links": "^5.1.10",
"storybook-addon-styled-component-theme": "^1.2.4",
"babel-loader": "^8.0.6"
"@storybook/addons": "^5.1.10",
"@storybook/react": "^5.1.10",
"babel-loader": "^8.0.6",
"concurrently": "^4.1.0",
"dotenv": "^6.2.0",
"nodemon": "^1.18.7",
"storybook-addon-styled-component-theme": "^1.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"start": "if-env NODE_ENV=production && npm run start:prod || npm run start:dev",
"start:prod": "node server.js",
"start:dev": "concurrently \"nodemon --ignore 'client/*'\" \"npm run client\"",
"client": "cd client && npm run start",
"seed": "node scripts/seedDB.js",
"install": "cd client && npm install",
"build": "cd client && npm run build",
"heroku-postbuild": "npm run build",
"storybook": "start-storybook",
"build-storybook": "build-storybook"
},
Expand Down
6 changes: 6 additions & 0 deletions routes/api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const router = require("express").Router();
const testRoutes = require("./test");

router.use("/test", testRoutes);

module.exports = router;
8 changes: 8 additions & 0 deletions routes/api/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const router = require("express").Router();
const testController = require("../../controllers/testController");

router
.route("/")
.get(testController.findAll)

module.exports = router;
11 changes: 11 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const path = require("path");
const router = require("express").Router();
const apiRoutes = require("./api");

router.use("/api", apiRoutes);

router.use((req, res) => {
res.sendFile(path.join(__dirname, "../client/build/index.html"));
});

module.exports = router;
30 changes: 30 additions & 0 deletions scripts/seedDB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require("../dbconfig/connection.js")();
const db = require("../models");

const randomHour = () => 3600000*(Math.random() * (10 - 1) + 1);
const seed = [
{
title: "Test 1",
date: new Date(Date.now() + randomHour)
},
{
title: "Test 2",
date: new Date(Date.now() + randomHour)
},
{
title: "Test 3",
date: new Date(Date.now() + randomHour)
}
];

db.Test
.remove({})
.then(() => db.Test.collection.insertMany(seed))
.then(data => {
console.log(data.result.n + " records inserted!");
process.exit(0);
})
.catch(err => {
console.error(err);
process.exit(1);
});
18 changes: 18 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const express = require("express");
const routes = require("./routes");
const server = express();
const PORT = process.env.PORT || 3001;
const mongooseConnect = require("./dbconfig/connection.js");

server.use(express.urlencoded({ extended: true }));
server.use(express.json());

if (process.env.NODE_ENV === "production")
server.use(express.static("client/build"));

server.use(routes);

server.listen(PORT, () => {
console.log(`Server running on PORT ${PORT}!`);
mongooseConnect();
});
Loading

0 comments on commit 99f4387

Please sign in to comment.