mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-14 12:09:04 +08:00
[express] Body parsing middleware included in 4.16 (#20624)
* express.json() and express.urlencoded() * Express: Add tslint.json
This commit is contained in:
@@ -1,109 +1,122 @@
|
||||
import * as express from 'express';
|
||||
|
||||
namespace express_tests {
|
||||
|
||||
var app = express();
|
||||
const app = express();
|
||||
|
||||
app.engine('jade', require('jade').__express);
|
||||
app.engine('html', require('ejs').renderFile);
|
||||
|
||||
express.static.mime.define({
|
||||
'application/fx': ['fx']
|
||||
'application/fx': ['fx']
|
||||
});
|
||||
app.use('/static', express.static(__dirname + '/public'));
|
||||
|
||||
// simple logger
|
||||
app.use(function(req, res, next) {
|
||||
console.log('%s %s', req.method, req.url);
|
||||
next();
|
||||
app.use((req, res, next) => {
|
||||
console.log('%s %s', req.method, req.url);
|
||||
next();
|
||||
});
|
||||
|
||||
app.use(function(err: any, req: express.Request, res: express.Response, next: express.NextFunction) {
|
||||
console.error(err);
|
||||
next(err);
|
||||
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');
|
||||
});
|
||||
|
||||
app.get('/', function(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 });
|
||||
|
||||
const pathStr: string = 'test';
|
||||
const pathStr = 'test';
|
||||
const pathRE: RegExp = /test/;
|
||||
const path = true ? pathStr : pathRE;
|
||||
|
||||
router.get(path);
|
||||
router.put(path)
|
||||
router.put(path);
|
||||
router.post(path);
|
||||
router.delete(path);
|
||||
router.get(pathStr);
|
||||
router.put(pathStr)
|
||||
router.put(pathStr);
|
||||
router.post(pathStr);
|
||||
router.delete(pathStr);
|
||||
router.get(pathRE);
|
||||
router.put(pathRE)
|
||||
router.put(pathRE);
|
||||
router.post(pathRE);
|
||||
router.delete(pathRE);
|
||||
|
||||
router.use((req, res, next) => { next(); })
|
||||
router.use((req, res, next) => { next(); });
|
||||
router.route('/users')
|
||||
.get((req, res, next) => {
|
||||
let types: string[] = req.accepts();
|
||||
let type: string | false = req.accepts('json');
|
||||
type = req.accepts(['json', 'text']);
|
||||
type = req.accepts('json', 'text');
|
||||
.get((req, res, next) => {
|
||||
const types: string[] = req.accepts();
|
||||
let type: string | false = req.accepts('json');
|
||||
type = req.accepts(['json', 'text']);
|
||||
type = req.accepts('json', 'text');
|
||||
|
||||
let charsets: string[] = req.acceptsCharsets();
|
||||
let charset: string | false = req.acceptsCharsets('utf-8');
|
||||
charset = req.acceptsCharsets(['utf-8', 'utf-16']);
|
||||
charset = req.acceptsCharsets('utf-8', 'utf-16');
|
||||
const charsets: string[] = req.acceptsCharsets();
|
||||
let charset: string | false = req.acceptsCharsets('utf-8');
|
||||
charset = req.acceptsCharsets(['utf-8', 'utf-16']);
|
||||
charset = req.acceptsCharsets('utf-8', 'utf-16');
|
||||
|
||||
let encodings: string[] = req.acceptsEncodings();
|
||||
let encoding: string | false = req.acceptsEncodings('gzip');
|
||||
encoding = req.acceptsEncodings(['gzip', 'deflate']);
|
||||
encoding = req.acceptsEncodings('gzip', 'deflate');
|
||||
const encodings: string[] = req.acceptsEncodings();
|
||||
let encoding: string | false = req.acceptsEncodings('gzip');
|
||||
encoding = req.acceptsEncodings(['gzip', 'deflate']);
|
||||
encoding = req.acceptsEncodings('gzip', 'deflate');
|
||||
|
||||
let languages: string[] = req.acceptsLanguages();
|
||||
let language: string | false = req.acceptsLanguages('en');
|
||||
language = req.acceptsLanguages(['en', 'ja']);
|
||||
language = req.acceptsLanguages('en', 'ja');
|
||||
const languages: string[] = req.acceptsLanguages();
|
||||
let language: string | false = req.acceptsLanguages('en');
|
||||
language = req.acceptsLanguages(['en', 'ja']);
|
||||
language = req.acceptsLanguages('en', 'ja');
|
||||
|
||||
// downcasting
|
||||
req.get('set-cookie') as undefined;
|
||||
req.get('set-cookie') as string[];
|
||||
const setCookieHeader1 = req.get('set-cookie');
|
||||
if (setCookieHeader1 !== undefined) {
|
||||
const setCookieHeader2: string[] = setCookieHeader1;
|
||||
}
|
||||
req.get('header') as undefined;
|
||||
req.get('header') as string;
|
||||
const header1 = req.get('header');
|
||||
if (header1 !== undefined) {
|
||||
const header2: string = header1;
|
||||
}
|
||||
// downcasting
|
||||
req.get('set-cookie') as undefined;
|
||||
req.get('set-cookie') as string[];
|
||||
const setCookieHeader1 = req.get('set-cookie');
|
||||
if (setCookieHeader1 !== undefined) {
|
||||
const setCookieHeader2: string[] = setCookieHeader1;
|
||||
}
|
||||
req.get('header') as undefined;
|
||||
req.get('header') as string;
|
||||
const header1 = req.get('header');
|
||||
if (header1 !== undefined) {
|
||||
const header2: string = header1;
|
||||
}
|
||||
|
||||
// upcasting
|
||||
const setCookieHeader3: string[] | undefined = req.get('set-cookie');
|
||||
const header3: string | undefined = req.header('header');
|
||||
// upcasting
|
||||
const setCookieHeader3: string[] | undefined = req.get('set-cookie');
|
||||
const header3: string | undefined = req.header('header');
|
||||
|
||||
req.headers.existingHeader as string;
|
||||
req.headers.nonExistingHeader as any as undefined;
|
||||
req.headers.existingHeader as string;
|
||||
req.headers.nonExistingHeader as any as undefined;
|
||||
|
||||
res.send(req.query['token']);
|
||||
});
|
||||
res.send(req.query['token']);
|
||||
});
|
||||
|
||||
router.get('/user/:id', function(req, res, next) {
|
||||
if (req.params.id == 0) next('route');
|
||||
else next();
|
||||
}, function(req, res, next) {
|
||||
res.render('regular');
|
||||
router.get('/user/:id', (req, res, next) => {
|
||||
if (Number(req.params.id) === 0) next('route');
|
||||
else next();
|
||||
}, (req, res, next) => {
|
||||
res.render('regular');
|
||||
});
|
||||
|
||||
app.use((req, res, next) => {
|
||||
// hacky trick, router is just a handler
|
||||
router(req, res, next);
|
||||
// hacky trick, router is just a handler
|
||||
router(req, res, next);
|
||||
});
|
||||
|
||||
app.use(router);
|
||||
@@ -120,9 +133,7 @@ namespace express_tests {
|
||||
***************************/
|
||||
import * as http from 'http';
|
||||
|
||||
|
||||
namespace node_tests {
|
||||
|
||||
{
|
||||
// http.createServer can take express application
|
||||
const app: express.Application = express();
|
||||
|
||||
16
types/express/index.d.ts
vendored
16
types/express/index.d.ts
vendored
@@ -14,6 +14,7 @@
|
||||
/// <reference types="express-serve-static-core" />
|
||||
/// <reference types="serve-static" />
|
||||
|
||||
import * as bodyParser from "body-parser";
|
||||
import * as serveStatic from "serve-static";
|
||||
import * as core from "express-serve-static-core";
|
||||
|
||||
@@ -23,12 +24,23 @@ import * as core from "express-serve-static-core";
|
||||
declare function e(): core.Express;
|
||||
|
||||
declare namespace e {
|
||||
/**
|
||||
* This is a built-in middleware function in Express. It parses incoming requests with JSON payloads and is based on body-parser.
|
||||
* @since 4.16.0
|
||||
*/
|
||||
var json: typeof bodyParser.json;
|
||||
|
||||
/**
|
||||
* This is the only built-in middleware function in Express. It serves static files and is based on serve-static.
|
||||
* This is a built-in middleware function in Express. It serves static files and is based on serve-static.
|
||||
*/
|
||||
var static: typeof serveStatic;
|
||||
|
||||
/**
|
||||
* This is a built-in middleware function in Express. It parses incoming requests with urlencoded payloads and is based on body-parser.
|
||||
* @since 4.16.0
|
||||
*/
|
||||
var urlencoded: typeof bodyParser.urlencoded;
|
||||
|
||||
export function Router(options?: RouterOptions): core.Router;
|
||||
|
||||
interface RouterOptions {
|
||||
@@ -60,7 +72,7 @@ declare namespace e {
|
||||
interface Handler extends core.Handler { }
|
||||
interface IRoute extends core.IRoute { }
|
||||
interface IRouter<T> extends core.IRouter { }
|
||||
interface IRouterHandler<T> extends core.IRouterHandler<T> { }
|
||||
interface IRouterHandler<T> extends core.IRouterHandler<T> { }
|
||||
interface IRouterMatcher<T> extends core.IRouterMatcher<T> { }
|
||||
interface MediaType extends core.MediaType { }
|
||||
interface NextFunction extends core.NextFunction { }
|
||||
|
||||
12
types/express/tslint.json
Normal file
12
types/express/tslint.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"extends": "dtslint/dt.json",
|
||||
"rules": {
|
||||
// Not sure if changing these won't break apps.
|
||||
"interface-name": false,
|
||||
"no-empty-interface": false,
|
||||
"no-namespace": false,
|
||||
"no-reference-import": false,
|
||||
"no-var-keyword": false,
|
||||
"strict-export-declare-modifiers": false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user