Skip to content

Instantly share code, notes, and snippets.

@tonysneed
Created November 13, 2016 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonysneed/18c02d68664a6b77d9eb1fe60c5114c8 to your computer and use it in GitHub Desktop.
Save tonysneed/18c02d68664a6b77d9eb1fe60c5114c8 to your computer and use it in GitHub Desktop.
Express server with static files support
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