Skip to content

Instantly share code, notes, and snippets.

@JacopoMangiavacchi
Created October 23, 2017 17:23
Show Gist options
  • Save JacopoMangiavacchi/4d56db708d439f184840dba37a839ffc to your computer and use it in GitHub Desktop.
Save JacopoMangiavacchi/4d56db708d439f184840dba37a839ffc to your computer and use it in GitHub Desktop.
TypeScript - Node.js Express.js Rest Test : <- Post <- Request <- Get
import * as express from 'express'
import * as bodyParser from "body-parser"
import * as request from 'request'
class Language {
language: string
constructor (language: string) {
this.language = language
}
}
interface Request {
url: string
}
class App {
public express: express.Express
constructor () {
this.express = express()
this.mountRoutes()
}
private mountRoutes (): void {
const router = express.Router()
router.get('/language', (req, res) => {
let language = new Language("TypeScript")
res.json(language)
})
router.post('/request', (req, res) => {
let passedRequest: Request = req.body as Request
request(passedRequest.url, (error: any, response: any, body: any) => {
if (!error) {
let language: Language = JSON.parse(body) as Language
res.json(language);
}
});
})
this.express.use(bodyParser.json())
this.express.use('/', router)
}
public listen(port: number): void {
this.express.listen(port, (err: any) => {
if (err) {
return console.log(err)
}
return console.log(`server is listening on port ${port}`)
})
}
}
let app = new App()
app.listen(8020)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment