Skip to content

Commit 074ca04

Browse files
committed
create starter project
0 parents commit 074ca04

File tree

10 files changed

+286
-0
lines changed

10 files changed

+286
-0
lines changed

.babelrc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"presets": [
3+
[
4+
"@babel/preset-env",
5+
{
6+
"targets": {
7+
"esmodules": true
8+
}
9+
}
10+
]
11+
]
12+
}

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
dist/
2+
frontend/
3+
node_modules/
4+
package-lock.json
5+
.env
6+
.vscode/
7+
.idea
8+
.DS_Store

.prettierrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"semi": true,
3+
"singleQuote": true,
4+
"printWidth": 80,
5+
"tabWidth": 2,
6+
"useTabs": false
7+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Node/Express.js starter supporting ES6 module

package.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "node-express-es6-starter",
3+
"version": "1.0.0",
4+
"private": true,
5+
"description": "Node/Express.js start project supporting es6 module",
6+
"main": "app.js",
7+
"scripts": {
8+
"start": "node dist/app.js",
9+
"dev": "nodemon src/app.js --exec babel-node",
10+
"build": "babel src -d dist --copy-files",
11+
"postinstall": "npm run build"
12+
},
13+
"keywords": [
14+
"Node",
15+
"Express",
16+
"RestAPI",
17+
"MongoDB",
18+
"Mongoose",
19+
"ES6",
20+
"Starter"
21+
],
22+
"license": "MIT",
23+
"dependencies": {
24+
"@trinsic/service-clients": "^1.1.5018",
25+
"axios": "^0.21.1",
26+
"body-parser": "^1.19.0",
27+
"cookie-parser": "^1.4.5",
28+
"cors": "^2.8.5",
29+
"debug": "^2.6.9",
30+
"dotenv": "^10.0.0",
31+
"express": "^4.17.1",
32+
"method-override": "^3.0.0",
33+
"mongoose": "^5.13.2",
34+
"morgan": "^1.10.0"
35+
},
36+
"devDependencies": {
37+
"@babel/cli": "^7.14.5",
38+
"@babel/core": "^7.14.6",
39+
"@babel/node": "^7.14.7",
40+
"@babel/preset-env": "^7.14.7",
41+
"nodemon": "^2.0.9"
42+
}
43+
}

src/app.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// set up ======================================================================
2+
import express from 'express';
3+
import mongoose from 'mongoose'; // mongoose for mongodb
4+
import dbConfig from './config/db.config'; // load the database config
5+
import cookieParser from 'cookie-parser';
6+
import logger from 'morgan';
7+
import cors from 'cors';
8+
9+
import noteRoutes from './routes/note.routes';
10+
11+
const port = process.env.PORT || 3000; // set the port
12+
const app = express(); // create our app w/ express
13+
app.use(cors());
14+
15+
// configuration ===============================================================
16+
mongoose
17+
.connect(dbConfig.localUrl, {
18+
// Connect to local MongoDB instance. A remoteUrl is also available (modulus.io)
19+
useNewUrlParser: true,
20+
useUnifiedTopology: true,
21+
})
22+
.then(() => {
23+
console.log('Successfully connected to the database');
24+
})
25+
.catch((err) => {
26+
console.log('Could not connect to the database. Exiting now...');
27+
process.exit();
28+
});
29+
30+
if (process.env.NODE_ENV === 'development') {
31+
app.use(logger('dev')); // log every request to the console
32+
}
33+
app.use(express.json()); // parse application/json
34+
app.use(express.urlencoded({ extended: false })); // parse application/x-www-form-urlencoded
35+
app.use(cookieParser()); // parse cookie header and populate req.cookies with an object keyed by the cookie names
36+
app.use(express.static('./public')); // set the static files location /public/img will be /img for users
37+
38+
// routes ======================================================================
39+
app.use('/', noteRoutes);
40+
41+
// listen for requests (start app with node server.js) =========================
42+
app.listen(port);
43+
console.log('Server is listening on port ' + port);
44+
45+
export default app;

src/config/db.config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"remoteUrl": "mongodb://node:nodeuser@mongo.onmodulus.net:27017/uwO3mypu",
3+
"localUrl": "mongodb://localhost:27017/easy-notes"
4+
}

src/controllers/note.controller.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import Note from '../models/note.model';
2+
3+
// Create and Save a new Note
4+
export const createNote = (req, res) => {
5+
// Validate request
6+
if (!req.body.content) {
7+
return res.status(400).send({
8+
message: 'Note content can not be empty',
9+
});
10+
}
11+
12+
// Create a Note
13+
const note = new Note({
14+
title: req.body.title || 'Untitled Note',
15+
content: req.body.content,
16+
});
17+
18+
// Save Note in the database
19+
note
20+
.save()
21+
.then((data) => {
22+
res.send(data);
23+
})
24+
.catch((err) => {
25+
res.status(500).send({
26+
message: err.message || 'Some error occurred while creating the Note.',
27+
});
28+
});
29+
};
30+
31+
// Retrieve and return all notes from the database.
32+
export const findAll = (req, res) => {
33+
Note.find()
34+
.then((notes) => {
35+
res.send(notes);
36+
})
37+
.catch((err) => {
38+
res.status(500).send({
39+
message: err.message || 'Some error occurred while retrieving notes.',
40+
});
41+
});
42+
};
43+
44+
// Find a single note with a noteId
45+
export const findOne = (req, res) => {
46+
Note.findById(req.params.noteId)
47+
.then((note) => {
48+
if (!note) {
49+
return res.status(404).send({
50+
message: 'Note not found with id ' + req.params.noteId,
51+
});
52+
}
53+
res.send(note);
54+
})
55+
.catch((err) => {
56+
if (err.kind === 'ObjectId') {
57+
return res.status(404).send({
58+
message: 'Note not found with id ' + req.params.noteId,
59+
});
60+
}
61+
return res.status(500).send({
62+
message: 'Error retrieving note with id ' + req.params.noteId,
63+
});
64+
});
65+
};
66+
67+
// Update a note identified by the noteId in the request
68+
export const updateNote = (req, res) => {
69+
// Validate Request
70+
if (!req.body.content) {
71+
return res.status(400).send({
72+
message: 'Note content can not be empty',
73+
});
74+
}
75+
76+
// Find note and update it with the request body
77+
Note.findByIdAndUpdate(
78+
req.params.noteId,
79+
{
80+
title: req.body.title || 'Untitled Note',
81+
content: req.body.content,
82+
},
83+
{ new: true }
84+
)
85+
.then((note) => {
86+
if (!note) {
87+
return res.status(404).send({
88+
message: 'Note not found with id ' + req.params.noteId,
89+
});
90+
}
91+
res.send(note);
92+
})
93+
.catch((err) => {
94+
if (err.kind === 'ObjectId') {
95+
return res.status(404).send({
96+
message: 'Note not found with id ' + req.params.noteId,
97+
});
98+
}
99+
return res.status(500).send({
100+
message: 'Error updating note with id ' + req.params.noteId,
101+
});
102+
});
103+
};
104+
105+
// Delete a note with the specified noteId in the request
106+
export const deleteNote = (req, res) => {
107+
Note.findByIdAndRemove(req.params.noteId)
108+
.then((note) => {
109+
if (!note) {
110+
return res.status(404).send({
111+
message: 'Note not found with id ' + req.params.noteId,
112+
});
113+
}
114+
res.send({ message: 'Note deleted successfully!' });
115+
})
116+
.catch((err) => {
117+
if (err.kind === 'ObjectId' || err.name === 'NotFound') {
118+
return res.status(404).send({
119+
message: 'Note not found with id ' + req.params.noteId,
120+
});
121+
}
122+
return res.status(500).send({
123+
message: 'Could not delete note with id ' + req.params.noteId,
124+
});
125+
});
126+
};

src/models/note.model.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import mongoose from 'mongoose';
2+
3+
const NoteSchema = mongoose.Schema(
4+
{
5+
title: String,
6+
content: String,
7+
},
8+
{
9+
timestamps: true,
10+
}
11+
);
12+
13+
export default mongoose.model('Note', NoteSchema);

src/routes/note.routes.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Router } from 'express';
2+
import {
3+
createNote,
4+
findAll,
5+
findOne,
6+
updateNote,
7+
deleteNote,
8+
} from '../controllers/note.controller';
9+
10+
const router = Router();
11+
12+
// Create a new Note
13+
router.post('/notes', createNote);
14+
15+
// Retrieve all Notes
16+
router.get('/notes', findAll);
17+
18+
// Retrieve a single Note with noteId
19+
router.get('/notes/:noteId', findOne);
20+
21+
// Update a Note with noteId
22+
router.put('/notes/:noteId', updateNote);
23+
24+
// Delete a Note with noteId
25+
router.delete('/notes/:noteId', deleteNote);
26+
27+
export default router;

0 commit comments

Comments
 (0)