├── .gitignore ├── src ├── controllers │ ├── PostController.ts │ └── UserController.ts ├── index.ts ├── config │ └── mongoose.ts ├── models │ ├── Post.ts │ └── User.ts ├── routes │ ├── indexRoutes.ts │ ├── UserRoutes.ts │ └── PostRoutes.ts └── server.ts ├── README.md ├── package.json └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | build -------------------------------------------------------------------------------- /src/controllers/PostController.ts: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/controllers/UserController.ts: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # REST API using Typescript 2 | 3 | - mongodb / mongoose 4 | - express / node -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import { Server } from "./server"; 2 | import './config/mongoose' 3 | 4 | const server = new Server(); 5 | server.start(); 6 | -------------------------------------------------------------------------------- /src/config/mongoose.ts: -------------------------------------------------------------------------------- 1 | import mongoose from "mongoose"; 2 | 3 | const MONGO_URI = "mongodb://localhost/restapits"; 4 | 5 | mongoose.set("useFindAndModify", false); 6 | mongoose.connect(MONGO_URI || process.env.MONGODB_URL, { 7 | useNewUrlParser: true, 8 | useCreateIndex: true, 9 | useUnifiedTopology: true, 10 | }); 11 | -------------------------------------------------------------------------------- /src/models/Post.ts: -------------------------------------------------------------------------------- 1 | import { Schema, model } from "mongoose"; 2 | 3 | const PostSchema = new Schema({ 4 | title: { type: String, required: true }, 5 | url: { type: String, required: true, unique: true, lowercase: true }, 6 | content: { type: String, required: true }, 7 | image: { type: String }, 8 | createdAt: { type: Date, default: Date.now }, 9 | updatedAt: Date, 10 | }); 11 | 12 | export default model("Post", PostSchema); 13 | -------------------------------------------------------------------------------- /src/models/User.ts: -------------------------------------------------------------------------------- 1 | import { Schema, model } from "mongoose"; 2 | 3 | const UserSchema = new Schema({ 4 | name: { type: String, required: true }, 5 | email: { type: String, required: true, unique: true }, 6 | password: { type: String, required: true }, 7 | username: { type: String, required: true, lowercase: true }, 8 | createdAt: { type: Date, default: Date.now() }, 9 | posts: [ 10 | { 11 | type: Schema.Types.ObjectId, 12 | ref: "Post", 13 | }, 14 | ], 15 | }); 16 | 17 | export default model("User", UserSchema); 18 | -------------------------------------------------------------------------------- /src/routes/indexRoutes.ts: -------------------------------------------------------------------------------- 1 | import { Request, Response, Router } from 'express'; 2 | 3 | class IndexRoutes { 4 | router: Router; 5 | 6 | constructor() { 7 | this.router = Router(); 8 | this.routes(); 9 | } 10 | 11 | public getIndex(req: Request, res: Response): void { 12 | res.json('Api: /api/posts'); 13 | } 14 | 15 | routes(): void { 16 | this.router.get('/', this.getIndex); 17 | } 18 | } 19 | 20 | const indexRoutes = new IndexRoutes(); 21 | indexRoutes.routes(); 22 | 23 | export default indexRoutes.router; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nodejs-typescript-rest-api", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "clean": "rimraf build", 8 | "build": "tsc", 9 | "dev": "ts-node-dev --respawn src/index.ts", 10 | "start": "node ./build/index.js" 11 | }, 12 | "keywords": [], 13 | "author": "", 14 | "license": "ISC", 15 | "devDependencies": { 16 | "@types/compression": "^1.7.0", 17 | "@types/cors": "^2.8.7", 18 | "@types/express": "^4.17.7", 19 | "@types/helmet": "0.0.48", 20 | "@types/mongoose": "^5.7.36", 21 | "@types/morgan": "^1.9.1", 22 | "@types/node": "^14.6.0", 23 | "nodemon": "^2.0.4", 24 | "rimraf": "^3.0.2", 25 | "typescript": "^4.0.2" 26 | }, 27 | "dependencies": { 28 | "compression": "^1.7.4", 29 | "cors": "^2.8.5", 30 | "express": "^4.17.1", 31 | "helmet": "^4.1.0", 32 | "mongoose": "^5.10.0", 33 | "morgan": "^1.10.0", 34 | "ts-node-dev": "^1.0.0-pre.60" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/server.ts: -------------------------------------------------------------------------------- 1 | import express from 'express'; 2 | import morgan from 'morgan'; 3 | import helmet from 'helmet'; 4 | import compression from 'compression'; 5 | import cors from 'cors'; 6 | 7 | // import routes 8 | import indexRoutes from './routes/indexRoutes'; 9 | import PostRouter from './routes/PostRoutes'; 10 | import UserRoutes from './routes/UserRoutes'; 11 | 12 | // Server Class 13 | class Server { 14 | public app: express.Application; 15 | 16 | constructor() { 17 | this.app = express(); 18 | this.config(); 19 | this.routes(); 20 | } 21 | 22 | public config(): void { 23 | // Settings 24 | this.app.set('port', process.env.PORT || 4000); 25 | // middlewares 26 | this.app.use(morgan('dev')); 27 | this.app.use(express.json()); 28 | this.app.use(express.urlencoded({extended: false})); 29 | this.app.use(helmet()); 30 | this.app.use(compression()); 31 | this.app.use(cors()); 32 | } 33 | 34 | public routes(): void { 35 | const router: express.Router = express.Router(); 36 | 37 | this.app.use('/', indexRoutes); 38 | this.app.use('/api/posts', PostRouter); 39 | this.app.use('/api/users', UserRoutes); 40 | } 41 | 42 | public start(): void { 43 | this.app.listen(this.app.get('port'), () => { 44 | console.log('Server is listenning on port', this.app.get('port')); 45 | }); 46 | } 47 | } 48 | 49 | export { Server } ; -------------------------------------------------------------------------------- /src/routes/UserRoutes.ts: -------------------------------------------------------------------------------- 1 | import { Request, Response, NextFunction, Router } from 'express'; 2 | 3 | import User from '../models/User'; 4 | 5 | class UserRouter { 6 | router: Router; 7 | 8 | constructor() { 9 | this.router = Router(); 10 | this.routes(); 11 | } 12 | 13 | async getUsers(req: Request, res: Response): Promise { 14 | const users = await User.find().populate('posts', 'title url -_id'); 15 | res.json(users); 16 | } 17 | 18 | async getUser(req: Request, res: Response): Promise { 19 | const user = await User.findById(req.params.id).populate('posts'); 20 | res.json(user); 21 | } 22 | 23 | async createUser(req: Request, res: Response): Promise { 24 | const newUser = new User(req.body); 25 | await newUser.save(); 26 | res.json({ status: 200, newUser }); 27 | } 28 | 29 | async updateUser(req: Request, res: Response): Promise { 30 | const { id } = req.params; 31 | const user = await User.findByIdAndUpdate(id, req.body, {new: true}); 32 | res.json(user); 33 | } 34 | 35 | async deleteUser(req: Request, res: Response): Promise { 36 | const { id } = req.params; 37 | const user = await User.findByIdAndRemove(id); 38 | res.json(user); 39 | } 40 | 41 | routes() { 42 | this.router.get('/', this.getUsers); 43 | this.router.get('/:id', this.getUser); 44 | this.router.post('/', this.createUser); 45 | this.router.put('/:id', this.updateUser); 46 | this.router.delete('/:id', this.deleteUser); 47 | } 48 | 49 | } 50 | 51 | const userRouter = new UserRouter(); 52 | export default userRouter.router; 53 | 54 | -------------------------------------------------------------------------------- /src/routes/PostRoutes.ts: -------------------------------------------------------------------------------- 1 | import { Router, Request, Response, NextFunction } from 'express'; 2 | 3 | import Post from '../models/Post'; 4 | 5 | class PostRouter { 6 | router: Router; 7 | 8 | constructor() { 9 | this.router = Router(); 10 | this.routes(); 11 | } 12 | 13 | public async getPosts(req: Request, res: Response): Promise { 14 | const posts = await Post.find(); 15 | res.json({ posts }); 16 | } 17 | 18 | public async getPost(req: Request, res: Response): Promise { 19 | const post = await Post.find({ url: { $regex: req.params.url } }); 20 | res.json(post); 21 | } 22 | 23 | public async createPost(req: Request, res: Response): Promise{ 24 | const { title, url, content, image } = req.body; 25 | const newPost= new Post({title, url, content, image}); 26 | await newPost.save(); 27 | res.json({status: res.status, data: newPost}); 28 | 29 | } 30 | 31 | public async updatePost(req: Request, res: Response): Promise{ 32 | const { url } = req.params; 33 | const post = await Post.findOneAndUpdate({url}, req.body); 34 | res.json({status: res.status, data: post}); 35 | } 36 | 37 | public async deletePost(req: Request, res: Response): Promise { 38 | await Post.findOneAndRemove({ url: req.params.url }); 39 | res.json({ response: 'Post deleted Successfully' }); 40 | } 41 | 42 | routes() { 43 | this.router.get('/', this.getPosts); 44 | this.router.get('/:url', this.getPost); 45 | this.router.post('/', this.createPost); 46 | this.router.put('/:url', this.updatePost); 47 | this.router.delete('/:url', this.deletePost); 48 | } 49 | } 50 | 51 | const postRoutes = new PostRouter(); 52 | 53 | export default postRoutes.router; -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | /* Basic Options */ 4 | "target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */ 5 | "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ 6 | // "lib": [], /* Specify library files to be included in the compilation. */ 7 | // "allowJs": true, /* Allow javascript files to be compiled. */ 8 | // "checkJs": true, /* Report errors in .js files. */ 9 | // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ 10 | // "declaration": true, /* Generates corresponding '.d.ts' file. */ 11 | // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ 12 | // "sourceMap": true, /* Generates corresponding '.map' file. */ 13 | // "outFile": "./", /* Concatenate and emit output to single file. */ 14 | "outDir": "./build", /* Redirect output structure to the directory. */ 15 | // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ 16 | // "composite": true, /* Enable project compilation */ 17 | // "removeComments": true, /* Do not emit comments to output. */ 18 | // "noEmit": true, /* Do not emit outputs. */ 19 | // "importHelpers": true, /* Import emit helpers from 'tslib'. */ 20 | // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ 21 | // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ 22 | 23 | /* Strict Type-Checking Options */ 24 | //"strict": true, /* Enable all strict type-checking options. */ 25 | // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ 26 | // "strictNullChecks": true, /* Enable strict null checks. */ 27 | // "strictFunctionTypes": true, /* Enable strict checking of function types. */ 28 | // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ 29 | // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ 30 | // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ 31 | // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ 32 | 33 | /* Additional Checks */ 34 | // "noUnusedLocals": true, /* Report errors on unused locals. */ 35 | // "noUnusedParameters": true, /* Report errors on unused parameters. */ 36 | // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ 37 | // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ 38 | 39 | /* Module Resolution Options */ 40 | // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ 41 | // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ 42 | // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ 43 | // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ 44 | // "typeRoots": [], /* List of folders to include type definitions from. */ 45 | // "types": [], /* Type declaration files to be included in compilation. */ 46 | // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ 47 | "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ 48 | // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ 49 | 50 | /* Source Map Options */ 51 | // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ 52 | // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ 53 | // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ 54 | // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ 55 | 56 | /* Experimental Options */ 57 | // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ 58 | // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ 59 | }, 60 | "include": [ 61 | "./src" 62 | ], 63 | "exclude": [ 64 | "node_modules" 65 | ] 66 | } --------------------------------------------------------------------------------