-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from warestack/feature/node-express-base
New backend seeds and documentation adjustments
- Loading branch information
Showing
46 changed files
with
790 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Node-Express Seeds (JavaScript Version) | ||
|
||
This repository contains multiple seeds (starter templates) for Node.js projects using Express.js with different | ||
configurations and additions. | ||
|
||
## Folder Structure | ||
|
||
- **node-js-express-starter**: This contains a basic Node-Express seed to get you started with a simple "Hello, World!" | ||
API. | ||
- **node-js-express-mongoose**: This seed incorporates Mongoose, enabling you to quickly start a project with MongoDB | ||
integration. | ||
|
||
## Getting Started | ||
|
||
To use any of the seeds, navigate to the respective folder and follow the instructions in its README. | ||
|
||
## Contributions | ||
|
||
Feel free to contribute to these seeds by submitting pull requests or opening issues with suggestions/improvements. |
2 changes: 2 additions & 0 deletions
2
backend/node-js-express/node-js-express-mongoose/.env.example
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
PORT=3000 | ||
MONGO_URI=mongodb+srv:// |
36 changes: 36 additions & 0 deletions
36
backend/node-js-express/node-js-express-mongoose/README.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Node Express - Mongoose with Javascript Seed | ||
|
||
This seed helps you kickstart your Node.js projects using Express.js and **Javascript** with integrated MongoDB support | ||
through Mongoose. Additionally, it includes a `healthz` endpoint, essential for automated deployments on Warestack. | ||
|
||
## Getting Started | ||
|
||
- Navigate to the `node-js-express-mongoose` directory. | ||
- Configure environment variables as described in the [local development](#local-development) section. | ||
- Ensure you have a MongoDB instance running or use your connection string in the `.env` for an external database. | ||
- Run `yarn install` to install the necessary dependencies. | ||
- Use `yarn start` to start the server. | ||
- Your API is now connected to a MongoDB instance, and you can begin creating models, routes, etc. | ||
|
||
## Dependencies | ||
|
||
This seed uses: | ||
|
||
- [Express.js](https://expressjs.com/): A fast, minimalist web framework for Node.js. | ||
- [Mongoose](https://mongoosejs.com/): Elegant MongoDB object modeling for Node.js. | ||
|
||
## Local Development | ||
|
||
This seed uses environment variables for configuration: | ||
|
||
- Make sure to set up your `.env` file by using `.env.example` as a reference. | ||
- Adjust the values in `.env` as necessary to match your setup. | ||
|
||
## Warestack Deployments | ||
|
||
For this Flask API to be compatible with automated deployments on Warestack, the inclusion of the `healthz` endpoint is | ||
mandatory. Warestack uses this endpoint to ensure that the service is running correctly. | ||
|
||
## Questions & Contributions | ||
|
||
If you have any questions or wish to contribute, please open an issue or submit a pull request. |
42 changes: 42 additions & 0 deletions
42
backend/node-js-express/node-js-express-mongoose/app/app.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const express = require('express'); | ||
|
||
// Load environment variables from .env file | ||
require('dotenv').config(); | ||
|
||
const dbConfig = require('./config/database'); | ||
|
||
const RootController = require('./root/root.controller'); | ||
const HealthController = require('./health/health.controller'); | ||
const UsersController = require('./user/user.controller'); | ||
|
||
class App { | ||
static bootstrap() { | ||
return new App(); | ||
} | ||
|
||
constructor() { | ||
// create express js application | ||
this.app = express(); | ||
|
||
// Connect to MongoDB | ||
dbConfig.connect(); | ||
|
||
// add routes | ||
this.routes(); | ||
} | ||
|
||
dbConnect(){ | ||
dbConfig.connect(); | ||
} | ||
routes() { | ||
this.app.use('/', new RootController().getRouter()); | ||
this.app.use('/healthz', new HealthController().getRouter()); | ||
this.app.use(`/users`, new UsersController().getRouter()); | ||
} | ||
} | ||
|
||
function expressApp() { | ||
return new App().app; | ||
} | ||
|
||
module.exports = expressApp; |
41 changes: 41 additions & 0 deletions
41
backend/node-js-express/node-js-express-mongoose/app/config/database.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const dbConfig = { | ||
connect: function() { | ||
const MONGO_URI = process.env.MONGO_URI || 'mongodb://localhost:27017/test'; // Default URI | ||
|
||
mongoose.connect(MONGO_URI, { | ||
useUnifiedTopology: true, | ||
useNewUrlParser: true, | ||
}) | ||
.then(() => { | ||
console.log('Successfully connected to MongoDB.'); | ||
}) | ||
.catch(error => { | ||
console.error('Error connecting to MongoDB:', error); | ||
process.exit(1); | ||
}); | ||
|
||
// Setting up mongoose event handlers | ||
mongoose.connection.on('error', err => { | ||
console.error('MongoDB connection error:', err); | ||
}); | ||
|
||
mongoose.connection.on('disconnected', () => { | ||
console.log('MongoDB disconnected.'); | ||
}); | ||
|
||
process.on('SIGINT', async () => { | ||
try { | ||
await mongoose.connection.close(); | ||
console.log('MongoDB connection closed due to app termination.'); | ||
process.exit(0); | ||
} catch (err) { | ||
console.error('Error during database disconnection:', err); | ||
process.exit(1); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = dbConfig; |
23 changes: 23 additions & 0 deletions
23
backend/node-js-express/node-js-express-mongoose/app/health/health.controller.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const express = require('express'); | ||
|
||
const HealthService = require('./health.service'); | ||
|
||
class HealthController { | ||
constructor() { | ||
this.router = express.Router(); | ||
|
||
// Setting the routes for the greeting endpoint group | ||
this.router.get('/', this.health.bind(this)); // Binding is necessary for 'this' to refer to the class instance | ||
} | ||
|
||
getRouter() { | ||
return this.router; | ||
} | ||
|
||
async health(request, response, next) { | ||
const message = HealthService.health(); | ||
response.status(200).send(message); | ||
} | ||
} | ||
|
||
module.exports = HealthController; |
7 changes: 7 additions & 0 deletions
7
backend/node-js-express/node-js-express-mongoose/app/health/health.service.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class HealthService { | ||
health() { | ||
return "I am healthy!"; | ||
} | ||
} | ||
|
||
module.exports = new HealthService(); // Exporting an instance of the service |
22 changes: 22 additions & 0 deletions
22
backend/node-js-express/node-js-express-mongoose/app/root/root.controller.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const express = require('express'); | ||
const RootService = require('./root.service'); | ||
|
||
class RootController { | ||
constructor() { | ||
this.router = express.Router(); | ||
|
||
// Setting the routes for the root endpoint group | ||
this.router.get('/', this.info.bind(this)); // Binding is necessary for 'this' to refer to the class instance | ||
} | ||
|
||
getRouter() { | ||
return this.router; | ||
} | ||
|
||
async info(request, response, next) { | ||
const message = RootService.info(); | ||
response.status(200).send(message); | ||
} | ||
} | ||
|
||
module.exports = RootController; |
7 changes: 7 additions & 0 deletions
7
backend/node-js-express/node-js-express-mongoose/app/root/root.service.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class RootService { | ||
info() { | ||
return "Hello, World!"; | ||
} | ||
} | ||
|
||
module.exports = new RootService(); // Exporting an instance of the service |
77 changes: 77 additions & 0 deletions
77
backend/node-js-express/node-js-express-mongoose/app/user/user.controller.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
const express = require('express'); | ||
|
||
const UserService = require('./user.service'); | ||
|
||
class UsersController { | ||
constructor() { | ||
this.router = express.Router(); | ||
this.router.get('/', this.getUsers.bind(this)); | ||
this.router.get('/:email', this.getUserByEmail.bind(this)); | ||
this.router.post('/', this.createUser.bind(this)); | ||
this.router.put('/:email', this.updateUser.bind(this)); | ||
this.router.delete('/:email', this.deleteUser.bind(this)); | ||
} | ||
|
||
getRouter() { | ||
return this.router; | ||
} | ||
|
||
async getUsers(request, response, next) { | ||
try { | ||
const users = await UserService.getUsers(); | ||
response.status(200).send(users); | ||
} catch (e) { | ||
next(e); | ||
} | ||
} | ||
|
||
async getUserByEmail(request, response, next) { | ||
try { | ||
const user = await UserService.getUserByEmail(request.params.email); | ||
if (!user) { | ||
response.status(404).send({ message: 'User not found.' }); | ||
return; | ||
} | ||
response.status(200).send(user); | ||
} catch (e) { | ||
next(e); | ||
} | ||
} | ||
|
||
async createUser(request, response, next) { | ||
try { | ||
const user = await UserService.createUser(request.body); | ||
response.status(201).send(user); | ||
} catch (e) { | ||
next(e); | ||
} | ||
} | ||
|
||
async updateUser(request, response, next) { | ||
try { | ||
const updatedUser = await UserService.updateUser(request.params.email, request.body); | ||
if (!updatedUser) { | ||
response.status(404).send({ message: 'User not found.' }); | ||
return; | ||
} | ||
response.status(200).send(updatedUser); | ||
} catch (e) { | ||
next(e); | ||
} | ||
} | ||
|
||
async deleteUser(request, response, next) { | ||
try { | ||
const deletedUser = await UserService.deleteUser(request.params.email); | ||
if (!deletedUser) { | ||
response.status(404).send({ message: 'User not found.' }); | ||
return; | ||
} | ||
response.status(200).send(deletedUser); | ||
} catch (e) { | ||
next(e); | ||
} | ||
} | ||
} | ||
|
||
module.exports = UsersController; |
15 changes: 15 additions & 0 deletions
15
backend/node-js-express/node-js-express-mongoose/app/user/user.model.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const userSchema = new mongoose.Schema({ | ||
name: { | ||
type: String, | ||
required: true | ||
}, | ||
email: { | ||
type: String, | ||
required: true, | ||
unique: true | ||
} | ||
}); | ||
|
||
module.exports = mongoose.model('User', userSchema); |
Oops, something went wrong.