Skip to content

Commit 5f0e654

Browse files
serverless example starter
0 parents  commit 5f0e654

File tree

10 files changed

+2565
-0
lines changed

10 files changed

+2565
-0
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# package directories
2+
node_modules
3+
jspm_packages
4+
5+
# Serverless directories
6+
.serverless
7+
8+
# Environment Variables
9+
.env

Controller/products.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
const serverless = require('serverless-http');
2+
const express = require('express');
3+
const app = express();
4+
5+
const bodyParser = require('body-parser');
6+
const uuid = require('uuid/v4');
7+
8+
const dbConnection = require('../dbConfigs');
9+
const ProductService = require('../Services/product');
10+
11+
12+
app.use(bodyParser.json());
13+
app.use(bodyParser.urlencoded({ extended: true }));
14+
15+
16+
// base url to test our API
17+
app.get('/index', async (req, res) => {
18+
await res.send("<h3>Welcome to the Product API for LogRocket Blog serverless Example!!</h3>")
19+
})
20+
21+
// function for creating a new product
22+
app.post('/', async (req, res) => {
23+
24+
try {
25+
26+
await dbConnection();
27+
28+
const data = req.body;
29+
30+
const {name, type, description, cost} = data;
31+
32+
if(!data) {
33+
return "Please pass all required fields!"
34+
}
35+
36+
const dataToSave = {name,type,description,cost,productId:uuid()};
37+
38+
let createProduct = await ProductService.createProduct(dataToSave);
39+
40+
if (createProduct) {
41+
return res.status(200).send(
42+
createProduct
43+
)
44+
}
45+
} catch (error) {
46+
// handle errors here
47+
console.log(error, "error!!");
48+
}
49+
50+
})
51+
52+
53+
// function for getting all products
54+
app.get('/', async (req, res) => {
55+
56+
try {
57+
await dbConnection();
58+
59+
const allProducts = await ProductService.getAllProduct();
60+
61+
if (allProducts) {
62+
return res.status(200).send({
63+
data: allProducts
64+
})
65+
}
66+
} catch (error) {
67+
// handle errors here
68+
console.log(error, "error!!");
69+
}
70+
})
71+
72+
73+
// function for getting a product by Id
74+
app.get('/:productId/', async (req, res) => {
75+
76+
try {
77+
78+
await dbConnection();
79+
80+
const {productId} = req.params;
81+
82+
const getProduct = await ProductService.getProductById({productId});
83+
84+
if(getProduct) {
85+
return res.status(200).send({
86+
data: getProduct
87+
})
88+
89+
}
90+
91+
} catch (error) {
92+
// handle errors here
93+
console.log(error, "error!!");
94+
95+
}
96+
97+
});
98+
99+
100+
module.exports.handler = serverless(app);

Model/product.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const mongoose = require("mongoose");
2+
3+
const ProductSchema = new mongoose.Schema (
4+
{
5+
name: {type: String},
6+
type: {type: String},
7+
cost: {type: Number},
8+
description: {type: String},
9+
productId: { type: String },
10+
},
11+
{timestamps: true}
12+
);
13+
14+
const ProductModel = mongoose.model("product",ProductSchema);
15+
16+
module.exports = ProductModel;
17+

Services/product.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const Product = require('../Model/product');
2+
3+
4+
module.exports = {
5+
6+
async createProduct (product) {
7+
8+
let result = await Product.create(product);
9+
if(result) {
10+
return {
11+
data: product,
12+
message: "Product successfully created!"
13+
};
14+
}
15+
16+
return "Error creating new product"
17+
18+
},
19+
20+
21+
async getAllProduct() {
22+
23+
let product = await Product.find();
24+
25+
if(product) return product;
26+
27+
return "Error fetching products from db"
28+
29+
},
30+
31+
32+
async getProductById(productId) {
33+
34+
let product = await Product.findOne(productId);
35+
36+
if(product) return product;
37+
38+
return "Error fetching product from db";
39+
40+
},
41+
42+
43+
};

dbconfigs.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const mongoose = require('mongoose');
2+
require("dotenv").config();
3+
mongoose.Promise = global.Promise;
4+
5+
const connectToDatabase = async () => {
6+
let isConnected;
7+
if (isConnected) {
8+
console.log('using existing database connection');
9+
return Promise.resolve();
10+
}
11+
12+
console.log('using new database connection');
13+
const database = await mongoose.connect(process.env.MONGODB_URL, {useNewUrlParser: true});
14+
isConnected = database.connections[0].readyState;
15+
// return isConnected;
16+
};
17+
18+
module.exports = connectToDatabase;
19+

0 commit comments

Comments
 (0)