├── .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 |
2 | 3 | ![shortstories-api](./ss-api.jpg) 4 | 5 | [`https://shortstories-api.onrender.com`](https://shortstories-api.onrender.com) 6 | 7 | An API that sends a random short story. This is a public API and no API key is required. \ 8 | Checkout the cool & minimal frontend of this API - [stories.io](https://storiesio.netlify.app) 9 | 10 | | ENDPOINT | METHOD | DESCRIPTION | 11 | | -------------------------- | ------- | -------------------------------------------------------------- | 12 | | [`/`](#get-) | **GET** | Returns a random short story from the database | 13 | | [`/stories`](#get-stories) | **GET** | Returns a list, containing all short stories from the database | 14 | 15 |
16 | 17 | 18 | ```ts 19 | { 20 | _id : ObjectId; // ID of the story 21 | title : String; // Title of the story 22 | author : String; // Author of the story 23 | story : String; // Entire story (plain text) 24 | moral : String; // Moral of the story 25 | } 26 | ``` 27 | 28 | --- 29 | 30 | ### GET `/` 31 | 32 | Returns a random short story from the database. 33 | 34 | **Request :** 35 | 36 | ```bash 37 | curl https://shortstories-api.onrender.com/ 38 | ``` 39 | 40 | **Response :** 41 | 42 | ```bash 43 | { 44 | "_id" : "5ff6fb389f24d116ce28d716", 45 | "title" : "Jupiter and the Monkey", 46 | "author" : "Aesop's Fables", 47 | "story" : "There was once a baby show among the Animals in the forest. Jupiter provided the prize. Of course all the proud mammas from far and near brought their babies. But none got there earlier than Mother Monkey. Proudly she presented her baby among the other contestants. As you can imagine, there was quite a laugh when the Animals saw the ugly flat-nosed, hairless, pop-eyed little creature. \"Laugh if you will,\" said the Mother Monkey. \"Though Jupiter may not give him the prize, I know that he is the prettiest, the sweetest, the dearest darling in the world.\"", 48 | "moral" : "Mother love is blind." 49 | } 50 | ``` 51 | 52 | ### GET `/stories` 53 | 54 | Returns a list, containing all short stories from the database. 55 | 56 | **Request :** 57 | 58 | ```bash 59 | curl https://shortstories-api.onrender.com/stories 60 | ``` 61 | 62 | **Response :** 63 | 64 | ```bash 65 | [ 66 | { 67 | "_id" : "5ff6fb389f24d116ce28d69f", 68 | "title" : "The Wolf in Sheep's Clothing", 69 | "author" : "Aesop's Fables", 70 | "story" : "A certain Wolf could not get enough to eat because of the watchfulness of the Shepherds. But one night he found a sheep skin that had been cast aside and forgotten. The next day, dressed in the skin, the Wolf strolled into the pasture with the Sheep. Soon a little Lamb was following him about and was quickly led away to slaughter. That evening the Wolf entered the fold with the flock. But it happened that the Shepherd took a fancy for mutton broth that very evening, and, picking up a knife, went to the fold. There the first he laid hands on and killed was the Wolf.", 71 | "moral" : "The evil doer often comes to harm through his own deceit." 72 | } 73 | ... 140 more 74 | ] 75 | ``` 76 | --------------------------------------------------------------------------------