├── .dockerignore ├── .gitignore ├── ss-api.jpg ├── Dockerfile ├── docker-compose.yml ├── package.json ├── LICENSE ├── index.js └── README.md /.dockerignore: -------------------------------------------------------------------------------- 1 | node_modules/ -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .env -------------------------------------------------------------------------------- /ss-api.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/poseidon-code/shortstories-api/HEAD/ss-api.jpg -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:latest 2 | 3 | WORKDIR /app 4 | COPY package.json . 5 | RUN npm install 6 | 7 | COPY . . 8 | EXPOSE 5000 9 | 10 | CMD ["npm", "run", "start"] -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.8' 2 | services: 3 | server: 4 | build: . 5 | image: poseidon-code/shortstories-api 6 | container_name: c_shortstories-api 7 | ports: 8 | - '5000:5000' 9 | volumes: 10 | - .:/app 11 | - ./node_modules 12 | env_file: 13 | - .env -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "shortstories-api", 3 | "version": "1.1.1", 4 | "description": "An API that sends a random short story.", 5 | "type": "module", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/poseidon-code/shortstories-api" 9 | }, 10 | "main": "index.js", 11 | "scripts": { 12 | "start": "node index.js", 13 | "dev": "nodemon index.js" 14 | }, 15 | "author": "poseidon-code", 16 | "license": "MIT", 17 | "dependencies": { 18 | "cors": "^2.8.5", 19 | "dotenv": "^8.2.0", 20 | "express": "^4.17.1", 21 | "helmet": "^6.0.0", 22 | "mongoose": "^6.6.0" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Pritam Halder 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import dotenv from "dotenv"; 2 | dotenv.config(); 3 | import express from "express"; 4 | import cors from "cors"; 5 | import mongoose from "mongoose"; 6 | import helmet from "helmet"; 7 | 8 | const app = express(); 9 | const PORT = process.env.PORT || 5000; 10 | const URI = `mongodb+srv://everyone:${process.env.EVERYONE}@stories.l6tlk.mongodb.net/stories?retryWrites=true&w=majority`; 11 | 12 | app.use(helmet()); 13 | app.use(cors()); 14 | app.use(express.json()); 15 | app.disable("x-powered-by"); 16 | 17 | // Connect MongoDB Atlas' stories Database 18 | mongoose.set("strictQuery", true); 19 | mongoose 20 | .connect(URI, { useNewUrlParser: true, useUnifiedTopology: true }) 21 | .then(() => { 22 | console.log("Database Connected"); 23 | }) 24 | .catch((err) => console.log(err)); 25 | 26 | // Stories Schema 27 | const Schema = mongoose.Schema; 28 | // prettier-ignore 29 | const StoriesSchema = new Schema({ 30 | title : String, 31 | author : String, 32 | story : String, 33 | moral : String, 34 | }); 35 | 36 | const Stories = mongoose.model("stories", StoriesSchema); 37 | 38 | // Routes 39 | // get random story 40 | app.get("/", async (_, res) => { 41 | try { 42 | const count = await Stories.countDocuments(); 43 | const random = Math.floor(Math.random() * count); 44 | const story = await Stories.findOne().skip(random); 45 | return res.status(200).send(story); 46 | } catch (error) { 47 | if (process.env.NODE_ENV == "development") { 48 | console.error(error); 49 | return res.status(500).send(error); 50 | } else { 51 | return res.status(500); 52 | } 53 | } 54 | }); 55 | 56 | // get all stories 57 | app.get("/stories", async (_, res) => { 58 | try { 59 | const stories = await Stories.find({}); 60 | return res.status(200).send(stories); 61 | } catch (error) { 62 | if (process.env.NODE_ENV == "development") { 63 | console.error(error); 64 | return res.status(500).send(error); 65 | } else { 66 | return res.status(500); 67 | } 68 | } 69 | }); 70 | 71 | app.listen(PORT, () => { 72 | console.log(`Server is running on port ${PORT}`); 73 | }); 74 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |