當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript plugins.bodyParser方法代碼示例

本文整理匯總了TypeScript中restify.plugins.bodyParser方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript plugins.bodyParser方法的具體用法?TypeScript plugins.bodyParser怎麽用?TypeScript plugins.bodyParser使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在restify.plugins的用法示例。


在下文中一共展示了plugins.bodyParser方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: create

export function create(config: any, callback?: () => void) {

    console.log ('Setup server at ' + config);

    let server = restify.createServer();

    server.use(restify.plugins.bodyParser());
    server.use(restify.plugins.queryParser())

    // Make it a web server
    server.get('/', (req, res, next) => {
        res.redirect('./public/test.html', next);
    });


    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();
    });
//.........這裏部分代碼省略.........
開發者ID:johnshew,項目名稱:nagbot,代碼行數:101,代碼來源:server.ts

示例2:

server.server = new http.Server();

server.address().port;
server.address().family;
server.address().address;

server.listen("somePath", send);
server.close();

server.use(restify.plugins.acceptParser(server.acceptable));
server.use(restify.plugins.authorizationParser());
server.use(restify.plugins.dateParser());
server.use(restify.plugins.queryParser());
server.use(restify.plugins.jsonp());
server.use(restify.plugins.gzipResponse());
server.use(restify.plugins.bodyParser());
server.use(restify.plugins.throttle({
    burst: 100,
    rate: 50,
    ip: true,
    overrides: {
        '192.168.1.1': {
            rate: 0,
            burst: 0
        }
    }
}));
server.use(restify.plugins.conditionalHandler([{
    contentType: ['text/plain'],
    handler: (req: restify.Request, res: restify.Response, next: restify.Next): void => {
        res.send('OK');
開發者ID:Jeremy-F,項目名稱:DefinitelyTyped,代碼行數:31,代碼來源:restify-tests.ts

示例3: require

import {createServer, plugins} from 'restify';
import corsMiddleware = require('restify-cors-middleware');

import {deleteSingle} from './delete-single';
import {getAll} from './get-all';
import {getSingle} from './get-single';
import {post} from './post';

var server = createServer();

// Add bodyParser plugin for parsing JSON in request body
server.use(plugins.bodyParser());

// Add CORS
const options: corsMiddleware.Options = {
    preflightMaxAge: 5,
    origins: ['*'],
    allowHeaders: ['API-Token'],
    exposeHeaders: ['API-Token-Expiry']
  };
const cors: corsMiddleware.CorsMiddleware = corsMiddleware(options);
server.pre(cors.preflight);
server.use(cors.actual);

// Add routes
server.get('/api/customers', getAll);
server.post('/api/customers', post);
server.get('/api/customers/:id', getSingle);
server.del('/api/customers/:id', deleteSingle);

server.listen(8080, () => console.log('API is listening'));
開發者ID:,項目名稱:,代碼行數:31,代碼來源:


注:本文中的restify.plugins.bodyParser方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。