本文整理匯總了TypeScript中restify.plugins.serveStatic方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript plugins.serveStatic方法的具體用法?TypeScript plugins.serveStatic怎麽用?TypeScript plugins.serveStatic使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類restify.plugins
的用法示例。
在下文中一共展示了plugins.serveStatic方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: crossOrigin
server.use(restify.plugins.queryParser());
server.use(
function crossOrigin(req,res,next){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
return next();
}
);
server.listen(PORT, () => {
console.log(`Extension server online on port ${PORT}`);
})
// Serve the bot content.
server.get('/', restify.plugins.serveStatic({
'directory': `${__dirname}/bot`,
'default': 'bot.html'
}));
// Opens URLs.
server.get('/open', (req, res, next) => {
if (!req.query.url) {
res.send(200);
return;
}
opn(req.query.url);
res.send(200);
});
// This is the function that gets run when the VS Code extension
// gets loaded. activate is triggered by a registered activation event
示例2: create
//.........這裏部分代碼省略.........
server.get('/api/v1.0/reminders', async (req, res, next) => {
let reminders = await remindersStore.find("j@s.c");
res.send(reminders);
return next();
});
server.post('/api/v1.0/reminders', async (req, res, next) => {
let user = "j@s.c";
req.body['user'] = user;
let reminder = new reminders.Reminder(req.body);
let update = await remindersStore.update(reminder);
res.header("Location", `/api/v1.0/reminders/${reminder.id}`);
res.send(201, reminder);
next();
});
server.get('/api/v1.0/reminders/:id', async (req, res, next) => {
let user = "j@s.c";
if (!req.params.hasOwnProperty('id') || typeof req.params.id != "string") {
res.send(400, "id not found");
next();
return;
}
let result = await remindersStore.get(req.params.id);
if (!result) {
res.send(404, "Not found.");
} else {
res.send(result);
}
next();
});
server.put('/api/v1.0/reminders/:id', async (req, res, next) => {
let user = "j@s.c";
req.body.user = user;
if (!req.params.hasOwnProperty('id') && typeof req.params.id != "string") {
res.send(400, "id not found");
next();
return;
}
let reminder = new reminders.Reminder(req.body, true);
let exists = await remindersStore.get(req.params.id);
let update = await remindersStore.update(reminder);
res.header("Location", `/api/v1.0/reminders/${reminder.id}`);
res.send(exists ? 200 : 201, reminder);
next();
});
server.patch('/api/v1.0/reminders/:id', async (req, res, next) => {
let user = "j@s.c";
if (!req.params.hasOwnProperty('id') && typeof req.params.id != "string") {
res.send(400, "id not found");
next();
return;
}
let reminder = await remindersStore.get(req.params.id);
let created = false;
if (!reminder) {
created = true;
let result = null;
reminder = new reminders.Reminder(req.body, true);
} else {
reminder.update(req.body);
}
let update = await remindersStore.update(reminder);
res.send(created ? 201 : 200, reminder);
next();
});
server.del('/api/v1.0/reminders/:id', async (req, res, next) => {
let user = "j@s.c";
if (!req.params.hasOwnProperty('id') && typeof req.params.id != "string") {
res.send(400, "id not found");
next();
return;
}
let reminder = await remindersStore.get(req.params.id);
if (!reminder) {
res.send(401, "Not found")
} else {
await remindersStore.delete(reminder);
res.send(200);
}
next();
});
// server.get(/\/public\/?.*/,
server.get("/public/*",
restify.plugins.serveStatic({directory: __dirname + '/..'})
);
server.listen(
config, () => {
console.log(`Server listening on ${server.url}`);
if (callback) callback();
});
return server;
}