Created
November 13, 2016 13:52
-
-
Save tonysneed/18c02d68664a6b77d9eb1fe60c5114c8 to your computer and use it in GitHub Desktop.
Express server with static files support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as express from "express"; | |
import { Request, Response, NextFunction } from "express"; | |
import * as bodyParser from "body-parser"; | |
import * as path from "path"; | |
import { productsRouter } from "./routes/products"; | |
namespace express_router { | |
let app = express(); | |
let staticRoot = path.join(__dirname, "../"); | |
let scripts = path.join(__dirname, "../../node_modules"); | |
let port: number = process.env.PORT || 3000; | |
// Server static files | |
app.use(express.static(staticRoot)); | |
app.use("/node_modules", express.static(scripts)); | |
// Plug in body parser middleware for posting JSON | |
app.use(bodyParser.json()); | |
// Add products router | |
app.use("/api/products", productsRouter); | |
app.listen(port, () => { | |
console.log(`Simple Express Web API listening on port ${port}`); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment