Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can Export the class and define middleware in typescript

I create the class user where i just only define the get method but when i call the class in middleware and use it, it not show any error but when i run the code it show server not found. when i del this line app.use(userRoute) my server work.

users.ts

import { NextFunction, Request, Response } from 'express';
import { Controller, Get, Req, Res } from 'routing-controllers'

@Controller()
class User {
    @Get('/signup')

    signUP(@Req() req: Request, @Res() res: Response, next: NextFunction) {
        return res.render('signup')

    };
}

export { User as userRoute }

app.ts

const express = require('express')
const path = require('path')
const app = express()
import { userRoute } from "./routes/user";

const bodyPaser = require('body-parser')


app.use(bodyPaser.urlencoded({ extended: true }))
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')


app.use(userRoute)

app.use('/', (req, res) => {

    res.write('<html lang="eng">');
    res.write('<head><title>Page</title><style>body{background-color: wheat; color: red; font-      size: 25px; padding-left: 250px;}.d{}</style></head>')
    res.write('<body><h1>It is working</h1></body>')
    res.write('</html>')
    return res.end()
});

app.listen('3000')
console.log('working')
like image 526
usman imtiaz Avatar asked Dec 03 '20 11:12

usman imtiaz


People also ask

How do I export a classes in TypeScript?

Use named exports to export multiple classes in TypeScript, e.g. export class A {} and export class B {} . The exported classes can be imported by using a named import as import {A, B} from './another-file' . You can have as many named exports as necessary in a file.

What is middleware in TypeScript?

A “middleware” in Express is just a function that intercepts every request so that you can return a custom response or apply a custom filter when a request reaches your server. You can also call the next function to pass on the request for processing (to another registered middleware).

How do I export an object in TypeScript?

export = and import = require() TypeScript supports export = to model the traditional CommonJS and AMD workflow. The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum.


1 Answers

create instance of your class before export. Try some like:

class User {
...
}

const userRoute =  new User()
export {userRoute}

Updated based in doc of routing-controllers

according to the github page of that library, the use in express should be like this:


@Controller()
export class UserController {

    @Get("/users")
    getAll(@Req() request: Request, @Res() response: Response) {
        return response.send("Hello response!");
    }

}


import "reflect-metadata";
import { useExpressServer } from "routing-controllers";

let express = require("express"); // or you can import it if you have installed typings
let app = express(); // your created express server
// app.use() // you can configure it the way you want
useExpressServer(app, { // register created express server in routing-controllers
    controllers: [UserController] // and configure it the way you need (controllers, validation, etc.)
});
app.listen(3000); // run your express server

take a look at their doc

like image 148
Jeterson Miranda Gomes Avatar answered Sep 18 '22 14:09

Jeterson Miranda Gomes