本文整理匯總了TypeScript中express.Express.set方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Express.set方法的具體用法?TypeScript Express.set怎麽用?TypeScript Express.set使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類express.Express
的用法示例。
在下文中一共展示了Express.set方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: 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);
});
};
示例2:
const applyApiMocks = (app: Express) => {
app.set('json spaces', 40);
app.get(path.resolve(CONFIG.API_PREFIX, 'title'), (request, response) =>
response.json({
data: appConstants.APP_TITLE,
}),
);
};
示例3: function
app.on('ready', function() {
var ifaces = os.networkInterfaces();
Object.keys(ifaces).forEach(function (ifname) {
if(host_url){
return true;
}
ifaces[ifname].forEach(function (iface) {
if(host_url){
return true;
}
if ('IPv4' !== iface.family || iface.internal !== false) {
// skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
return;
}
if(!host_url){
host_url = "http://"+iface.address+":"+PORT+"/";
}
});
});
mainWindow = electronWindow.createWindow({width: 800, height: 600});
mainWindow.showUrl(path.resolve(path_to_root, 'index.html'), {
'role_url': host_url
}, () => {
console.log('window is now visible!');
//mainWindow.webContents.openDevTools();
});
mainWindow.on('closed', function () {
mainWindow = null; // let GC run its course
});
//Staic file server
server = express();
server.use(compression());
if(MINIFY_ASSETS) {
server.use(minify());
}
server.use(express.static(path.join(path_to_root, 'assets'), { maxAge: 1000*60*60*24*7 }));
server.use(express.static(path.join(path_to_root, 'generated')));
server.set('view engine', 'ejs');
server.get('/', function(req, res){
res.render('index', {title: 'Title data', role: 'list'});
});
server.get('/:role', function(req, res, next){
let role = req.params.role;
res.render(role, {role: role}, function(err, html) {
if (!err) {
return res.send(html);
} else {
// Not a view, skip this resolver
next();
}
});
});
//Start server
let httpServer = http.createServer(server);
httpServer.listen(PORT, function(){
// Print out our actual IP Address so they know what to tell their friends :D
console.log("Listening on "+host_url);
});
game = new Game();
game.net = new ServerNet(game, httpServer);
game.initShip('ship1');
if(DEV && os.platform() !== 'win32') {
let replInstance = repl.start({'prompt': 'node> '});
replInstance.on('exit', () => {
app.quit();
});
replInstance.context.game = game;
replInstance.context.server = server;
}
});
示例4: request
import {NextFunction} from "express";
import {Response} from "express";
import {Request} from "express";
import {Express} from "express";
import {HttpError} from "http-errors";
request("http://api.factoriomods.com/mods", function(err: Error, res: Response, body: string) {
let mods = JSON.parse(body);
mods.sort((a, b) => { if (a.id > b.id) { return 1; } else if (a.id < b.id) { return -1; } else { return 0; } })
.forEach((mod) => { console.log(Mod.parseFromJson(JSON.stringify((mod)))); });
});
let app: Express = 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"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
app.use("/", index.router);
app.use("/users", users.router);
// catch 404 and forward to error handler
app.use(function(req: Request, res: Response, next: NextFunction) {