Server static html file .
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 '/'
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?
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);
Stale issue message
Closing this as a working solution was provided. Feel free open a feature request if you'd like to have a different solution.
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.