-
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.
feature: Introduce backend seeds and documentation improvements
- Loading branch information
1 parent
de96fd6
commit dff69a0
Showing
53 changed files
with
623 additions
and
146 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
8 changes: 5 additions & 3 deletions
8
backend/node-express/README.md → backend/node-express-js/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
File renamed without changes.
36 changes: 36 additions & 0 deletions
36
backend/node-express-js/node-express-mongoose-js/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. | ||
|
||
## Getting Started | ||
|
||
- Navigate to the `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. |
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
File renamed without changes.
23 changes: 23 additions & 0 deletions
23
backend/node-express-js/node-express-mongoose-js/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-express-js/node-express-mongoose-js/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-express-js/node-express-mongoose-js/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-express-js/node-express-mongoose-js/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 |
76 changes: 76 additions & 0 deletions
76
backend/node-express-js/node-express-mongoose-js/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,76 @@ | ||
const express = require('express'); | ||
const UserService = require('../services/users.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-express-js/node-express-mongoose-js/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); |
45 changes: 45 additions & 0 deletions
45
backend/node-express-js/node-express-mongoose-js/app/user/user.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,45 @@ | ||
const User = require('../models/user'); | ||
|
||
class UserService { | ||
async getUsers() { | ||
try { | ||
return await User.find(); | ||
} catch (err) { | ||
throw new Error('Error fetching users from the database.'); | ||
} | ||
} | ||
|
||
async getUserByEmail(email) { | ||
try { | ||
return await User.findOne({ email: email }); | ||
} catch (err) { | ||
throw new Error('Error fetching user by email.'); | ||
} | ||
} | ||
|
||
async createUser(user) { | ||
try { | ||
return await User.create(user); | ||
} catch (err) { | ||
throw new Error('Error creating user.'); | ||
} | ||
} | ||
|
||
async updateUser(email, user) { | ||
try { | ||
return await User.findOneAndUpdate({ email: email }, user, { new: true }); | ||
} catch (err) { | ||
throw new Error('Error updating user.'); | ||
} | ||
} | ||
|
||
async deleteUser(email) { | ||
try { | ||
return await User.findOneAndDelete({ email: email }); | ||
} catch (err) { | ||
throw new Error('Error deleting user.'); | ||
} | ||
} | ||
} | ||
|
||
module.exports = new UserService(); // Exporting an instance of the service |
4 changes: 2 additions & 2 deletions
4
backend/node-express/mongoose/package.json → ...-js/node-express-mongoose-js/package.json
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
File renamed without changes.
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 @@ | ||
PORT=3000 |
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 @@ | ||
# Hello World - Node Express with Javascript Seed | ||
|
||
This seed is a basic starter for building an Express.js API with **JavaScript** that simply responds with "Hello, | ||
World!". | ||
|
||
## Getting Started | ||
|
||
- Navigate to the `node-express-starter-js` directory. | ||
- Configure environment variables as described in the [local development](#local-development) section. | ||
- Run `yarn install` to install the necessary dependencies. | ||
- Use `yarn start` to start the server. | ||
- Access the API at `http://localhost:3000`. You should receive a "Hello, World!" message. | ||
- The `healthz` endpoint at http://localhost:3000/healthz provides a health check response which is crucial for | ||
Warestack's deployment automation. | ||
|
||
## Dependencies | ||
|
||
This seed uses: | ||
|
||
- [Express.js](https://expressjs.com/): A fast, minimalist web framework 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. |
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
Oops, something went wrong.