本文整理匯總了TypeScript中express-handlebars類的典型用法代碼示例。如果您正苦於以下問題:TypeScript express-handlebars類的具體用法?TypeScript express-handlebars怎麽用?TypeScript express-handlebars使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了express-handlebars類的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: create
export async function create() {
var app: express.Express = express();
// TODO: move to new express typings. blocked by res.send(obj); file issue
//var app: express.Application = express();
// app.set('views', path.join(__dirname, 'views'));
// app.set('view engine', 'hjs');
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
// TODO: hook up logger
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// -------------------------------------------------------
// routes
// -------------------------------------------------------
await routes.setup(app);
// -------------------------------------------------------
// error handling
// -------------------------------------------------------
// forward routes not found to the error handler
app.use((req: express.Request, res: express.Response, next: express.NextFunction) => {
let err = new Error('Not Found');
err['status'] = 404;
next(err);
});
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use((err, req: express.Request, res: express.Response, next: express.NextFunction) => {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use((err, req: express.Request, res: express.Response, next: express.NextFunction) => {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
return app;
}
示例2: Express
.then((): httpServer | null => {
const app: Express.Application = Express();
app.engine("handlebars", exphbs({defaultLayout: "main"}));
app.set("view engine", "handlebars");
const router: Express.Router = Express.Router();
addRoutes(router);
app.use("/", router);
return app.listen(port);
})
示例3: initRoutes
export async function initRoutes(app: Express) {
console.log('Init routes...');
app.set('trust proxy', 'loopback');
app.use(compression());
app.use(bodyParser.json());
await db.init();
stats.init();
app.post('/vote', (req, res) => {
const vote: Vote = req.body;
if (isVoteValid(vote)) {
stats.addVote(vote);
db.saveVote(extend(vote, {
date: new Date(),
ip: req.ip,
userAgent: req.get('User-Agent')
})).then(oldVote => {
if (oldVote) {
stats.removeVote(oldVote);
}
});
} else {
console.log('Invalid vote', vote);
}
res.json({});
});
app.get('/stats', (_req, res) => {
res.set('Cache-Control', 'max-age=' + 1);
res.json(stats.getStats());
});
app.use(express.static('public'));
app.set('views', './server/views');
app.engine('handlebars', exphbs({ defaultLayout: 'main' }));
app.set('view engine', 'handlebars');
app.get(/^\/$|\/index.html/, (req, res) => {
renderWithClientToken('index', req, res);
});
app.get('/iframe.html', (req, res) => {
renderWithClientToken('iframe', req, res);
});
};
示例4: setupTemplating
async function setupTemplating(app: express.Application) {
app.engine(
'.hbs',
exphbs({
defaultLayout: 'main',
extname: '.hbs',
helpers: await loadHandlebarsHelpers()
})
);
app.set('view engine', '.hbs');
app.use((req, res, next) => {
let { url, query } = req;
res.locals.request = { url, query };
next();
});
}
示例5: exphbs
var env = process.env.NODE_ENV || 'development';
app.locals.ENV = env;
app.locals.ENV_DEVELOPMENT = (env === 'development');
//ignore HTTPS errors
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
// view engine setup
app.engine('handlebars', exphbs({
layoutsDir: settings.root + '/views/layouts/',
defaultLayout: settings.layoutFile,
partialsDir: [settings.root +'/views/partials/'],
helpers: {
scrollSpy: helpers.scrollSpy,
siteDirectory: helpers.siteDirectory,
breadCrumb: helpers.breadCrumb,
dirEntry: helpers.dirEntry
},
compilerOptions: {
preventIndent: true
}
}));
app.set('views', settings.root + '/views');
app.set('view engine', 'handlebars');
app.use(favicon(settings.root + '/public/img/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
示例6: require
if (!WallTime.rules) {
var tzData = require("chronoshift/lib/walltime/walltime-data.js");
WallTime.init(tzData.rules, tzData.zones);
}
import { VERSION, DATA_SOURCE_MANAGER, HIDE_GITHUB_ICON, HEADER_BACKGROUND } from './config';
import * as plywoodRoutes from './routes/plywood/plywood';
var app = express();
app.disable('x-powered-by');
// view engine setup
const viewsDir = path.join(__dirname, '../../src/views');
app.engine('.hbs', handlebars({
defaultLayout: 'main',
extname: '.hbs',
layoutsDir: path.join(viewsDir, 'layouts'),
partialsDir: path.join(viewsDir, 'partials')
}));
app.set('views', viewsDir);
app.set('view engine', '.hbs');
app.use(compress());
app.use(logger('dev'));
app.use(express.static(path.join(__dirname, '../../build/public')));
app.use(express.static(path.join(__dirname, '../../assets')));
app.use(bodyParser.json());
app.get('/', (req: Request, res: Response, next: Function) => {
DATA_SOURCE_MANAGER.getQueryableDataSources().then((dataSources) => {
示例7: express
import {registerBookCopiesRoutes} from "./controllers/bookCopyController";
import {LoanController} from "./controllers/loanController";
let app = express();
let api = express();
app.use("/api", api);
app.use(require("morgan")("dev"));
DbConnection.initConnection(app.get("env"));
app.engine("handlebars", handlebars({
defaultLayout: "main",
partialsDir: "views/partials",
helpers: {
section: function (name, options) {
if (!this._sections) {
this._sections = {};
}
this._sections[name] = options.fn(this);
return null;
}
}
}));
app.set("view engine", "handlebars");
app.set("port", process.env.PORT || 5000);
app.use(cookieParser(credentials.cookieSecret));
app.use(expressSession());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
//TODO.md: domains are deprecated and will be removed in the future,
//find an alternative