mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-23 21:00:01 +08:00
[express] http.createServer can take express app (#11292)
* Create pug-test.ts * Test http.createServer can take express app * Delete this
This commit is contained in:
committed by
Masahiro Wakame
parent
cf6fef6e8a
commit
e0e86e3535
@@ -1,101 +1,114 @@
|
||||
/// <reference path="express.d.ts" />
|
||||
/// <reference path="../node/node.d.ts" />
|
||||
|
||||
import * as express from 'express';
|
||||
var app = express();
|
||||
|
||||
app.engine('jade', require('jade').__express);
|
||||
app.engine('html', require('ejs').renderFile);
|
||||
|
||||
express.static.mime.define({
|
||||
'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(function(err: any, req: express.Request, res: express.Response, next: express.NextFunction) {
|
||||
console.error(err);
|
||||
next(err);
|
||||
});
|
||||
|
||||
|
||||
app.get('/', function(req, res){
|
||||
res.send('hello world');
|
||||
});
|
||||
namespace express_tests {
|
||||
|
||||
const router = express.Router();
|
||||
var app = express();
|
||||
|
||||
app.engine('jade', require('jade').__express);
|
||||
app.engine('html', require('ejs').renderFile);
|
||||
|
||||
const pathStr : string = 'test';
|
||||
const pathRE : RegExp = /test/;
|
||||
const path = true? pathStr : pathRE;
|
||||
express.static.mime.define({
|
||||
'application/fx': ['fx']
|
||||
});
|
||||
app.use('/static', express.static(__dirname + '/public'));
|
||||
|
||||
router.get(path);
|
||||
router.put(path)
|
||||
router.post(path);
|
||||
router.delete(path);
|
||||
router.get(pathStr);
|
||||
router.put(pathStr)
|
||||
router.post(pathStr);
|
||||
router.delete(pathStr);
|
||||
router.get(pathRE);
|
||||
router.put(pathRE)
|
||||
router.post(pathRE);
|
||||
router.delete(pathRE);
|
||||
|
||||
router.use((req, res, next) => { next(); })
|
||||
router.route('/users')
|
||||
.get((req, res, next) => {
|
||||
let types: string[] = req.accepts();
|
||||
let type: string | boolean = req.accepts('json');
|
||||
type = req.accepts(['json', 'text']);
|
||||
type = req.accepts('json', 'text');
|
||||
|
||||
let charsets: string[] = req.acceptsCharsets();
|
||||
let charset: string | boolean = 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 | boolean = req.acceptsEncodings('gzip');
|
||||
encoding = req.acceptsEncodings(['gzip', 'deflate']);
|
||||
encoding = req.acceptsEncodings('gzip', 'deflate');
|
||||
|
||||
let languages: string[] = req.acceptsLanguages();
|
||||
let language: string | boolean = req.acceptsLanguages('en');
|
||||
language = req.acceptsLanguages(['en', 'ja']);
|
||||
language = req.acceptsLanguages('en', 'ja');
|
||||
|
||||
res.send(req.query['token']);
|
||||
// simple logger
|
||||
app.use(function(req, res, next) {
|
||||
console.log('%s %s', req.method, req.url);
|
||||
next();
|
||||
});
|
||||
|
||||
router.get('/user/:id', function(req, res, next) {
|
||||
if (req.params.id == 0) next('route');
|
||||
else next();
|
||||
}, function(req, res, next) {
|
||||
res.render('regular');
|
||||
});
|
||||
app.use(function(err: any, req: express.Request, res: express.Response, next: express.NextFunction) {
|
||||
console.error(err);
|
||||
next(err);
|
||||
});
|
||||
|
||||
app.use((req, res, next) => {
|
||||
// hacky trick, router is just a handler
|
||||
router(req, res, next);
|
||||
});
|
||||
|
||||
app.use(router);
|
||||
app.get('/', function(req, res) {
|
||||
res.send('hello world');
|
||||
});
|
||||
|
||||
app.listen(3000);
|
||||
const router = express.Router();
|
||||
|
||||
const next: express.NextFunction = () => {};
|
||||
const nextWithArgument: express.NextFunction = (err: any) => {};
|
||||
|
||||
/**
|
||||
* The express.Application is compatible with http.createServer
|
||||
*/
|
||||
const pathStr: string = 'test';
|
||||
const pathRE: RegExp = /test/;
|
||||
const path = true ? pathStr : pathRE;
|
||||
|
||||
router.get(path);
|
||||
router.put(path)
|
||||
router.post(path);
|
||||
router.delete(path);
|
||||
router.get(pathStr);
|
||||
router.put(pathStr)
|
||||
router.post(pathStr);
|
||||
router.delete(pathStr);
|
||||
router.get(pathRE);
|
||||
router.put(pathRE)
|
||||
router.post(pathRE);
|
||||
router.delete(pathRE);
|
||||
|
||||
router.use((req, res, next) => { next(); })
|
||||
router.route('/users')
|
||||
.get((req, res, next) => {
|
||||
let types: string[] = req.accepts();
|
||||
let type: string | boolean = req.accepts('json');
|
||||
type = req.accepts(['json', 'text']);
|
||||
type = req.accepts('json', 'text');
|
||||
|
||||
let charsets: string[] = req.acceptsCharsets();
|
||||
let charset: string | boolean = 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 | boolean = req.acceptsEncodings('gzip');
|
||||
encoding = req.acceptsEncodings(['gzip', 'deflate']);
|
||||
encoding = req.acceptsEncodings('gzip', 'deflate');
|
||||
|
||||
let languages: string[] = req.acceptsLanguages();
|
||||
let language: string | boolean = req.acceptsLanguages('en');
|
||||
language = req.acceptsLanguages(['en', 'ja']);
|
||||
language = req.acceptsLanguages('en', 'ja');
|
||||
|
||||
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');
|
||||
});
|
||||
|
||||
app.use((req, res, next) => {
|
||||
// hacky trick, router is just a handler
|
||||
router(req, res, next);
|
||||
});
|
||||
|
||||
app.use(router);
|
||||
|
||||
app.listen(3000);
|
||||
|
||||
const next: express.NextFunction = () => { };
|
||||
}
|
||||
|
||||
/***************************
|
||||
* *
|
||||
* Test with other modules *
|
||||
* *
|
||||
***************************/
|
||||
import * as http from 'http';
|
||||
http.createServer(app);
|
||||
|
||||
|
||||
namespace node_tests {
|
||||
|
||||
{
|
||||
// http.createServer can take express application
|
||||
const app: express.Application = express();
|
||||
http.createServer(app).listen(5678);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user