let initialize = function(app){ app.set("views", config.root + "/server/views"); app.engine(".hbs", exphbs({ defaultLayout: "main", extname: ".hbs", // in the feature we probably don"t need the next 2 lines // https://github.com/ericf/express-handlebars/issues/147#issuecomment-159737839 layoutsDir: config.root + "/server/views/layouts/", partialsDir: config.root + "/server/views/partials/", // ensure the javascript is at the bottom of the code in express-handlebars template // http://stackoverflow.com/a/25307270 // helpers: { // section: function(name, options){ // if (!this._sections) { this._sections = {}; }; // this._sections[name] = options.fn(this); // return null; // } // } })); app.set("view engine", ".hbs"); if (config.env === "production" || config.env === "staging") { // app.use(favicon(path.join(config.root, "client", "favicon.ico"))); // app.use(express.static(path.join(config.root, "client"), { index: "_" })); // // app.use(morgan("tiny", { // like "dev" but no colors // // //skip: function(req, res) { return res.statusCode < 400 }, // // stream: logger.stream })); } else { // development // // app.use(favicon(path.join(config.root, "client", "favicon.ico"))); // // if you are happy with a browser plugin, then you don"t need this middleware // // live-reload corrupts pdf files: http://stackoverflow.com/a/28091651/2726725 // app.use(require("connect-livereload")({ // port: 35729, // default=35729 // ignore: [/print/] // all that contains "print": https://github.com/intesso/connect-livereload#options // })); // without last argument express serves index.html even when my routing is to a different file: // http://stackoverflow.com/a/25167332/2726725 // It is also recommended to put static middleware first: http://stackoverflow.com/a/28143812/2726725 // Have this pb. only when I try to serve another jade page as homepage app.use("/", express.static(path.join(config.root, "client"), { index: "_"} )); app.use("/node_modules", express.static(path.join(config.root, "node_modules"))); // to serve node_modules // app.use(morgan("dev", { stream: logger.stream })); // app.use(morgan("dev")); } // add a second static source for static files: // http://stackoverflow.com/questions/5973432/setting-up-two-different-static-directories-in-node-js-express-framework app.use("/public", express.static(path.join(config.root, "server/public"))); // for js files used by some views app.use("/views", express.static(path.join(config.root, "server/views"))); allRoutes(app); return app; };
// configure app to use bodyParser() // this will let us get the data from a POST app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); app.use(methodOverride("_method")); app.use(methodOverride("X-HTTP-Method-Override")); app.use(favicon(__dirname + "/public/favicon.ico")); app.use("/bower_components", express.static(__dirname + "/bower_components")); //Routes app.use('/api/v1', apiRoutes.getRoutes(app, express.Router())); app.use("/", webroutes.getRoutes(app, express.Router())); //File Paths app.set('views', path.join(__dirname, 'views')); app.engine('.hbs', exphbs({defaultLayout: 'main', extname: '.hbs'})); app.set('view engine', '.hbs'); app.use(express.static(path.join(__dirname, "public"))); passportConfig.ConfigurePassport(app); /**************************************/ /* Logs */ /**************************************/ // create a write stream (in append mode) var logStream = fs.createWriteStream(__dirname + '/morganLog.log', { flags: 'a' }) // You can set morgan to log differently depending on your environment if (app.get('env') == 'production') { app.use(morgan('common', { skip: function (req, res) {
'use strict'; import express = require('express'); import path = require('path'); import _ = require('lodash'); const config = require('../app-config'); const app = express(); // let favicon = require('serve-favicon'); // app.use(favicon(__dirname + '/../client/favicon.ico')); var hbs = require('express-handlebars'); app.engine('handlebars', hbs()); app.set('view engine', 'handlebars'); app.set('views', 'server/views/'); app.use('/systemjs.config.js', express.static(path.resolve(__dirname, '../systemjs.config.js'))); app.use('/client', express.static(path.resolve(__dirname, '../client'))); app.use('/node_modules', express.static(path.resolve(__dirname, '../node_modules'))); const bodyParser = require('body-parser'); app.use(bodyParser.json()); // app.use(bodyParser.urlencoded({ extended: true })); const arangojs = require('arangojs'); const username = config.arangodb.username; const password = config.arangodb.password; const url = `http://${username}:${password}@localhost:8529`; const db = arangojs({ url, databaseName: 'think' });
(() => { "use strict"; const path = require("path"); let config = require('../config/config.json'); config.serverRoot = path.join(__dirname, '../'); const routes = require(config.serverRoot + 'config/routes.json'); const fs = require("fs"); const http = require('http'); const https = require('https'); const bodyParser = require('body-parser') const express = require('express'); const exphbs = require('express-handlebars'); const app = express(); //set view engine if (config.handlebarsTemplateEngine) { app.set('views', config.serverRoot + '/views'); const exphbsOptions = { defaultLayout: 'main', layoutsDir: config.serverRoot + '/views/layouts', partialsDir: config.serverRoot + '/views/partials', extname: ".view.html" }; app.engine('.view.html', exphbs(exphbsOptions)); app.set('view engine', '.view.html'); app.enable('view cache'); } //setting the static folder fo the app app.use(express.static(path.join(config.serverRoot, '../src/public'))); //routing //loop through the routes.json file //connecting the right controller //for each route and create it routes.forEach((route) => { const controller = require(config.serverRoot + '/controllers/' + route.controller + '.ctrl'); let middlewares = []; //load middlewares if exist for this route if (typeof route.middlewares !== "undefined" && route.middlewares.length) { route.middlewares.forEach((midName) => { const m = require(config.serverRoot + '/middlewares/' + midName + '.mid.ts'); middlewares.push(m); }); } app.all(route.path, middlewares, controller); }); //reducing the http header size //by removing x-powered-by app.disable('x-powered-by'); //set the http server if (config.httpServer) { const httpServer = http.createServer(app); httpServer.listen(config.httpPort, () => { console.log(`${config.appName} http server listening on port ${config.httpPort}`); }); } //set the https server if (config.httpsServer) { const sslConfig = { key: fs.readFileSync(config.serverRoot + 'config/ssl/file.pem'), cert: fs.readFileSync(config.serverRoot + 'config/ssl/file.crt') }; const httpsServer = https.createServer(sslConfig, app); httpsServer.listen(config.httpsPort, () => { console.log(`${config.appName} https server listening on port ${config.httpsPort}`); }); } })();
import * as express from "express"; import * as path from "path"; var expressHandlebars = require("express-handlebars"); import index from "./controllers/index"; const app: express.Express = express(); app.engine("handlebars", expressHandlebars({defaultLayout: "main"})); app.set("view engine", "handlebars"); app.use(express.static(path.join(__dirname, "public"))); app.use("/", index); if (app.get("env") === "development") { app.use((error: any, req, res, next) => { res.status(error["status"] || 500); res.render("error", { message: error.message, error }); }); }; app.use((error: any, req, res, next) => { res.status(error["status"] || 500); res.render("error", { message: error.message, error: {} });