Skip to content

Instantly share code, notes, and snippets.

@pmhegdek
pmhegdek / App.ts
Last active March 3, 2019 11:46
src/App.ts
import * as express from 'express'
class App {
public express
constructor() {
this.express = express()
this.loadRoutes()
}
private loadRoutes() : void {
const router = express.Router()
@pmhegdek
pmhegdek / server.ts
Created March 3, 2019 11:48
src/server.ts
import app from './App'
app.listen(3000, (err) => {
if (err) return console.log(err)
return console.log('Server is running in port: ', 3000);
})
{
"name": "ts-server",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"build": "./node_modules/.bin/tsc",
"start": "npm run build && node dist/server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "dist",
"sourceMap": true
},
"include": [
"src/**/*.ts"
@pmhegdek
pmhegdek / User.ts
Created March 9, 2019 06:43
ts-server-oop-example/src/models/User.ts
class User {
public name: String
public email: String
public role: String
private pwd: String
private ID: Number
constructor()
constructor(email: String)
constructor(name: String, email: String)
@pmhegdek
pmhegdek / Teacher.ts
Created March 9, 2019 06:52
ts-server-oop-example/src/models/Teacher.ts
import User from './User'
import Student from './Student'
class Teacher extends User {
constructor()
constructor(email: String)
constructor(name: String, email: String)
constructor(name: String, email: String, pwd: String)
constructor(name?: String, email?: String, pwd?: String) {
super(name, email, pwd, 'teacher')
@pmhegdek
pmhegdek / Student.ts
Created March 9, 2019 06:53
ts-server-oop-example/src/models/Student.ts
import User from './User'
class Student extends User {
constructor()
constructor(email: String)
constructor(name: String, email: String)
constructor(name: String, email: String, pwd: String)
constructor(name?: String, email?: String, pwd?: String) {
super(name, email, pwd, 'student')
}
@pmhegdek
pmhegdek / index.ts
Created March 9, 2019 07:04
ts-server-oop-example/src/routes/index.ts
import * as express from 'express'
import Teacher from './../models/Teacher'
import Student from './../models/Student'
import User from '../models/User';
import JSONResponse from '../libs/JSONResponse'
const router = express.Router()
router.post('/register', (req, res) => {
try {
let user: User;
@pmhegdek
pmhegdek / App.ts
Created March 9, 2019 07:18
ts-server-oop-example/src/App.ts
import * as express from 'express'
import router from './routes/index'
import * as bodyParser from 'body-parser';
class App {
public express
constructor() {
this.express = express()
this.express.use(bodyParser.json({ limit: '50mb' }))
this.loadRoutes()
}
@pmhegdek
pmhegdek / JSONResponse.ts
Created March 9, 2019 07:22
ts-server-oop-example/src/libs/JSONResponse.ts
class JSONResponse {
constructor() {}
static success(req, res, message, data) {
res.status(200).json({
code: 200,
message: message || 'success',
data: data,
});
}