本文整理汇总了TypeScript中express.Application.set方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Application.set方法的具体用法?TypeScript Application.set怎么用?TypeScript Application.set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类express.Application
的用法示例。
在下文中一共展示了Application.set方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: elasticsearchInit
export function elasticsearchInit(app: Application) {
app.set("elasticsearchClient", client);
client.indices
.exists({ index })
.then(indexExists => {
if (indexExists) {
return client.indices.delete({ index });
}
})
.then(() => client.indices.create({ index }))
.then(() =>
User.findAll().then(users => {
users.map(user => user.dataValues).forEach(user => {
client.index({
index,
type: "user",
id: user.id,
body: {
first_name: user.first_name,
last_name: user.last_name,
location: user.location
}
});
});
})
);
}
示例2: configure
private configure(): void {
this.app.set("port", this.normalizePort(process.env.PORT || "3000"));
Currencies.updateCurrencyRate();
process.on("exit", (code) => {
clearTimeout(this.timer);
process.exit(code);
}).on("SIGABRT", () => {
clearTimeout(this.timer);
process.exit(0);
}).on("SIGINT", () => {
clearTimeout(this.timer);
process.exit(0);
});
this.timer = Currencies.runRequestLoop(config.get("CURRENCY_UPDATE_TIMEOUT"));
}
示例3: init
export default async function init(app: Application) {
const sequelize = new Sequelize({
database: "signafire",
dialect: "sqlite",
storage: "./users.sqlite",
username: "root",
password: "",
logging: false,
modelPaths: [__dirname + "/models"],
operatorsAliases,
define: {
freezeTableName: true
}
});
app.set("sequelizeClient", sequelize);
const readUserData: () => Promise<any[]> = () =>
new Promise((resolve, reject) => {
fs.readFile("./data.json", "utf8", (err, data) => {
if (err) return reject(err);
resolve(JSON.parse(data));
});
});
// Sync to the database && populate data
// Return app afterwards to chain with elasticsearch init
return sequelize
.sync({ force: NODE_ENV !== "production" })
.then(readUserData)
.then(users => Promise.all(users.map(user => User.create(user))))
.then(users => {
console.log(users);
return app;
});
}
示例4: connect
import * as express from "express";
import { Application, Request, Response } from "express";
import { OK } from "http-status-codes";
import * as scaffoldRouter from "express-mongoose-scaffold";
import { connect } from "mongoose";
import { render as formRenderer } from "cms-forms";
import { render as gridRenderer } from "cms-grids";
import * as path from "path";
import { model as FoodsModel } from "./models/something";
import { model as CategoriesModel } from "./models/category";
connect(process.env.MONGO_URL || "mongodb://localhost/auto-router-demo");
let app :Application = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.get('/', (request : Request, response : Response) => {
response.status(OK).send("Hello World");
});
app.use('/admin/categories', scaffoldRouter(CategoriesModel).adminRouter);
app.use('/admin/foods', scaffoldRouter(FoodsModel).adminRouter);
app.use('/api/foods', scaffoldRouter(FoodsModel).apiRouter);
app.use(formRenderer);
app.use(gridRenderer);
app.use((req, res, next) => {
if (res.html) {
示例5: default
/**
* Sets up the EJS templating engine.
*/
export default (app: Application) => {
app.set('views', path.join(__dirname, '..', '..', 'views'));
app.set('view engine', 'ejs');
};
示例6: express
import * as mongoose from 'mongoose';
import {IRequest} from './interfaces';
import {router as authenticateRoutes} from './routes/authenticate';
import {router as usersRoutes} from './routes/users';
import {router as videosRoutes} from './routes/videos';
import {dbName, dbHost} from "./config";
namespace Play {
'use strict';
export const app: Application = express();
// view engine setup
// app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
// app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
// use bodyParser middleware to decode json parameters
app.use(bodyParser.json());
app.use(bodyParser.json({type: 'application/vnd.api+json'}));
// use bodyParser middleware to decode urlencoded parameters
app.use(bodyParser.urlencoded({extended: false}));
app.use(ExpressValidator());
// use cookieParser to extract cookie information from request
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
const corsHeaders: Object = {
示例7: async
export default async (app: Application) => {
if (config.database) {
mongoose.connect(
config.database.params.connection_string,
{
useNewUrlParser: true,
},
);
}
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
if (config.sessions) {
app.use(clientSessions(config.sessions));
}
if (config.templates) {
app.engine(config.templates.type, (await import(config.templates.type)).default.__express);
app.set("view engine", config.templates.type);
}
if (config.pages) {
config.pages.forEach((pageConfig: IPage) => {
app.get(pageConfig.route, (_req: IRequest, res: Response) => {
// req.headers
if (pageConfig.template) {
res.render(pageConfig.template, pageConfig.parameters);
} else {
res.status(200).json(pageConfig.parameters);
}
});
});
}
if (config.static) {
app.use(expressStatic(`__dirname${config.static}`));
}
if (config.collections) {
config.collections.forEach((schema: ISchema) => {
const modelSchema = model(schema);
app.get(`/${schema.name}`, async (_req: IRequest, res: Response) =>
res.status(200).json(await modelSchema.find()),
);
app.get(`/${schema.name}/:id`, async (req: IRequest, res: Response) => {
try {
res.status(200).json(await modelSchema.findOne({_id: req.params.id}));
} catch (err) {
res.status(404).json({error: "Not Found"});
}
});
app.post(`/${schema.name}`, async (req: IRequest, res: Response) => {
try {
const newModel = new modelSchema(req.body);
await newModel.save();
res.status(201).json(newModel);
} catch (err) {
res.status(400).json({error: err.message});
}
});
/*
app.put(`/${schema.name}/:id`, async (req: IRequest, res: Response) => {
//
});
app.patch(`/${schema.name}/:id`, async (req: IRequest, res: Response) => {
//
});
app.delete(`/${schema.name}/:id`, (req: IRequest, res: Response) => {
//
});
*/
});
}
/*if (config.auth) {
config.auth.forEach((authConfig: IAuthConfig) => {
//
});
}*/
};