本文整理汇总了TypeScript中express.json函数的典型用法代码示例。如果您正苦于以下问题:TypeScript json函数的具体用法?TypeScript json怎么用?TypeScript json使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了json函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: makeHttpServer
export function makeHttpServer(config: Config) {
const app = express()
const server = new AdminAPI(config)
app.use(expressWinston.logger({
winstonInstance: logger
}))
app.use(cors())
app.post(/\/v1\/admin\/reload/, (req: express.Request, res: express.Response) => {
return server.checkAuthorization(req.headers['authorization'])
.then((authResult) => {
if (!authResult) {
return { statusCode: 403, status: { error: 'forbidden' } }
}
return server.handleReload()
})
.then(reloadStatus => writeResponse(res, reloadStatus.status, reloadStatus.statusCode))
})
app.get(/\/v1\/admin\/config/, (req: express.Request, res: express.Response) => {
return server.checkAuthorization(req.headers['authorization'])
.then((authResult) => {
if (!authResult) {
return { statusCode: 403, status: { error: 'forbidden' } }
}
return server.handleGetConfig()
})
.then((configData) => writeResponse(res, configData.status, configData.statusCode))
})
app.post(
/\/v1\/admin\/config/, express.json(),
(req: express.Request, res: express.Response) => {
return server.checkAuthorization(req.headers['authorization'])
.then((authResult) => {
if (!authResult) {
return { statusCode: 403, status: { error: 'forbidden' } }
}
const newConfig = req.body
return server.handleSetConfig(newConfig)
})
.then((configStatus) => writeResponse(res, configStatus.status, configStatus.statusCode))
})
return app
}
示例2: function
app.get('/', function (req, res) {
res.status(200).send('ok');
});
var server = app.listen(3000, function () {
var port = server.address().port;
console.log('Example app listening at port %s', port);
});
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
import stylus = require('stylus');
app.use(stylus.middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/about', routes.about);
示例3: next
app.use((req, res, next) => {
console.log('%s %s', req.method, req.url);
next();
});
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
console.error(err);
next(err);
});
app.get('/', (req, res) => {
res.send('hello world');
});
// Accept json app-wide or on one endpoint.
app.use(express.json({ limit: "200kb" }));
app.post('/echo', express.json(), (req, res) => {
res.json(req.body);
});
// Accept urlencoded app-wide or on one endpoint.
app.use(express.urlencoded({
extended: false,
parameterLimit: 16
}));
app.post('/search', express.urlencoded(), (req, res) => {
res.json(Object.keys(req.body));
});
const router = express.Router({ caseSensitive: true, mergeParams: true, strict: true });
示例4: function
app.basePath = "/Users/arein/node/letterapp";
} else {
app.basePath = "/var/www/letterapp";
}
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', 'http://localhost:9000');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
};
// all environments
app.use(express.limit('70mb'));
app.use(express.json({limit: '70mb'}));
app.use(express.urlencoded({limit: '70mb'}));
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.set('view options', {pretty: config.isProd()});
app.locals.pretty = !config.isProd();
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
if (!config.isProd()) {
app.use(allowCrossDomain);
示例5: makeHttpServer
export function makeHttpServer(config: HubConfigInterface): { app: express.Application, server: HubServer, driver: DriverModel } {
const app: express.Application = express()
// Handle driver configuration
let driver: DriverModel
if (config.driverInstance) {
driver = config.driverInstance
} else if (config.driverClass) {
driver = new config.driverClass(config)
} else if (config.driver) {
const driverClass = getDriverClass(config.driver)
driver = new driverClass(config)
} else {
throw new Error('Driver option not configured')
}
const proofChecker = new ProofChecker(config.proofsConfig)
const server = new HubServer(driver, proofChecker, config)
// Instantiate server logging with Winston
app.use(expressWinston.logger({
winstonInstance: logger }))
app.use(cors())
// sadly, express doesn't like to capture slashes.
// but that's okay! regexes solve that problem
app.post(/^\/store\/([a-zA-Z0-9]+)\/(.+)/, (
req: express.Request,
res: express.Response
) => {
let filename = req.params[1]
if (filename.endsWith('/')){
filename = filename.substring(0, filename.length - 1)
}
const address = req.params[0]
server.handleRequest(address, filename, req.headers, req)
.then((publicURL) => {
writeResponse(res, { publicURL }, 202)
})
.catch((err: any) => {
logger.error(err)
if (err instanceof errors.ValidationError) {
writeResponse(res, { message: err.message, error: err.name }, 401)
} else if (err instanceof errors.AuthTokenTimestampValidationError) {
writeResponse(res, { message: err.message, error: err.name }, 401)
} else if (err instanceof errors.BadPathError) {
writeResponse(res, { message: err.message, error: err.name }, 403)
} else if (err instanceof errors.NotEnoughProofError) {
writeResponse(res, { message: err.message, error: err.name }, 402)
} else if (err instanceof errors.ConflictError) {
writeResponse(res, { message: err.message, error: err.name }, 409)
} else {
writeResponse(res, { message: 'Server Error' }, 500)
}
})
})
app.delete(/^\/delete\/([a-zA-Z0-9]+)\/(.+)/, (
req: express.Request,
res: express.Response
) => {
let filename = req.params[1]
if (filename.endsWith('/')){
filename = filename.substring(0, filename.length - 1)
}
const address = req.params[0]
server.handleDelete(address, filename, req.headers)
.then(() => {
res.writeHead(202)
res.end()
})
.catch((err: any) => {
logger.error(err)
if (err instanceof errors.ValidationError) {
writeResponse(res, { message: err.message, error: err.name }, 401)
} else if (err instanceof errors.AuthTokenTimestampValidationError) {
writeResponse(res, { message: err.message, error: err.name }, 401)
} else if (err instanceof errors.BadPathError) {
writeResponse(res, { message: err.message, error: err.name }, 400)
} else if (err instanceof errors.DoesNotExist) {
writeResponse(res, { message: err.message, error: err.name }, 404)
} else if (err instanceof errors.NotEnoughProofError) {
writeResponse(res, { message: err.message, error: err.name }, 402)
} else {
writeResponse(res, { message: 'Server Error' }, 500)
}
})
})
app.post(
/^\/list-files\/([a-zA-Z0-9]+)\/?/, express.json(),
(req: express.Request, res: express.Response) => {
// sanity check...
if (parseInt(req.headers['content-length']) > 4096) {
writeResponse(res, { message: 'Invalid JSON: too long'}, 400)
//.........这里部分代码省略.........
示例6: constructor
constructor() {
this.router = Router()
// Parse incoming requests with JSON payloads http://expressjs.com/en/api.html
this.router.use(express.json({ limit: '50mb' }))
}