routing-controllers icon indicating copy to clipboard operation
routing-controllers copied to clipboard

Server static html file .

Open TalissonJunior opened this issue 9 years ago • 5 comments

hey there,

How can i server a static html files with routing-controllers ?,

i tried:


import "reflect-metadata"; // this shim is required
import { createExpressServer } from "routing-controllers";
import { Log } from "./Core/Utils/Log";
import { serveStatic } from "serve-static";

const port = normalizePort(process.env.PORT || 3000);
const docsPath = __dirname + '/Areas/HelpPage/index.html';

// creates express app, registers all controller routes and returns you express app instance
const app = createExpressServer({
    routePrefix: "/api",
    controllers: [
        __dirname + "/Controller/*.js"
    ]
});


// serve static html file here -- but didn´t work.
app.use('/', serveStatic(docsPath));
app.listen(port);

const log = new Log();

log.success(`Server running on port ${port}`)

function normalizePort(val: number | string): number | string | boolean {
    let port: number = (typeof val === 'string') ? parseInt(val, 10) : val;
    if (isNaN(port)) return val;
    else if (port >= 0) return port;
    else return false;
}

i also tried to render as you can see below, but it isn´t what i want, i want to display an static html file when user goes to a route that is not defined.

import {Get, Res, JsonController, Render } from "routing-controllers";


@JsonController()
export class AppController {

    constructor(){}

    @Get("/")
    @Render(__dirname + "../Areas/HelpPage/index.html")
    Api(@Res() response:any) {}

}

But didn´t work, any ideas? is there a way to serve static files for default route like '/'

TalissonJunior avatar Oct 12 '17 01:10 TalissonJunior

const app = createExpressServer(...) behave just like a big express router with error handling, so you can do whatever you want like with normal express. What is the "not working"? The app.use("/" after routing-controllers doesn't get called? Have you tried move it up and use useExpressServer?

MichalLytek avatar Oct 12 '17 15:10 MichalLytek

it is working as like this snippet code

import * as express from 'express';

let expressApp = express();

useExpressServer(expressApp, {
.........
});

expressApp.use(express.static(path.join(__dirname, 'public')));

expressApp.listen(PORT);

jeffhieun avatar Aug 16 '18 10:08 jeffhieun

Stale issue message

github-actions[bot] avatar Feb 13 '20 00:02 github-actions[bot]

Closing this as a working solution was provided. Feel free open a feature request if you'd like to have a different solution.

attilaorosz avatar Feb 16 '22 16:02 attilaorosz

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Mar 19 '22 00:03 github-actions[bot]