ultimate-express 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/test.yml +27 -0
- package/EXPRESS_LICENSE +26 -0
- package/README.md +269 -0
- package/package.json +76 -0
- package/src/application.js +295 -0
- package/src/index.js +23 -0
- package/src/middlewares.js +95 -0
- package/src/request.js +324 -0
- package/src/response.js +686 -0
- package/src/router.js +490 -0
- package/src/utils.js +285 -0
- package/src/view.js +103 -0
- package/src/workers/fs.js +14 -0
- package/tests/index.js +64 -0
- package/tests/parts/.test/index.html +11 -0
- package/tests/parts/big.jpg +0 -0
- package/tests/parts/index.art +12 -0
- package/tests/parts/index.dot +12 -0
- package/tests/parts/index.ejs +12 -0
- package/tests/parts/index.handlebars +2 -0
- package/tests/parts/index.html +12 -0
- package/tests/parts/index.mustache +12 -0
- package/tests/parts/index.pug +6 -0
- package/tests/parts/index.swig +12 -0
- package/tests/parts/layouts/main.handlebars +12 -0
- package/tests/preload.cjs +5 -0
- package/tests/singular.js +41 -0
- package/tests/tests/app/app-engine.js +8 -0
- package/tests/tests/app/app-on-mount.js +24 -0
- package/tests/tests/app/app-path.js +21 -0
- package/tests/tests/app/app-route.js +30 -0
- package/tests/tests/app/options.js +16 -0
- package/tests/tests/app/setting-inheritance.js +16 -0
- package/tests/tests/engines/art-template.js +33 -0
- package/tests/tests/engines/dot.js +28 -0
- package/tests/tests/engines/ejs.js +25 -0
- package/tests/tests/engines/handlebars.js +28 -0
- package/tests/tests/engines/mustache.js +28 -0
- package/tests/tests/engines/pug.js +25 -0
- package/tests/tests/engines/swig.js +28 -0
- package/tests/tests/errors/error-handling-middleware.js +22 -0
- package/tests/tests/errors/next-error-optimized.js +26 -0
- package/tests/tests/errors/next-error.js +26 -0
- package/tests/tests/errors/unexpected-error-handling.js +18 -0
- package/tests/tests/listen/listen-random.js +11 -0
- package/tests/tests/listen/listen-specific.js +17 -0
- package/tests/tests/middlewares/body-json.js +31 -0
- package/tests/tests/middlewares/body-raw-deflate.js +43 -0
- package/tests/tests/middlewares/body-raw-gzip.js +43 -0
- package/tests/tests/middlewares/body-raw.js +41 -0
- package/tests/tests/middlewares/body-text.js +27 -0
- package/tests/tests/middlewares/body-urlencoded.js +30 -0
- package/tests/tests/middlewares/cookie-parser-signed.js +31 -0
- package/tests/tests/middlewares/cookie-parser.js +28 -0
- package/tests/tests/middlewares/cookie-session.js +40 -0
- package/tests/tests/middlewares/cors.js +29 -0
- package/tests/tests/middlewares/errorhandler.js +26 -0
- package/tests/tests/middlewares/express-fileupload-temp.js +46 -0
- package/tests/tests/middlewares/express-fileupload.js +28 -0
- package/tests/tests/middlewares/express-rate-limit.js +33 -0
- package/tests/tests/middlewares/express-session.js +37 -0
- package/tests/tests/middlewares/express-static-options.js +72 -0
- package/tests/tests/middlewares/express-static.js +40 -0
- package/tests/tests/middlewares/method-override.js +33 -0
- package/tests/tests/middlewares/multer.js +43 -0
- package/tests/tests/middlewares/multiple-middlewares.js +37 -0
- package/tests/tests/middlewares/response-time.js +29 -0
- package/tests/tests/middlewares/serve-index.js +38 -0
- package/tests/tests/middlewares/serve-static.js +38 -0
- package/tests/tests/middlewares/vhost.js +50 -0
- package/tests/tests/params/array-param.js +30 -0
- package/tests/tests/params/nested-params.js +24 -0
- package/tests/tests/params/param-errors.js +56 -0
- package/tests/tests/params/param-function.js +49 -0
- package/tests/tests/params/param-next-route.js +48 -0
- package/tests/tests/params/param-optimized.js +38 -0
- package/tests/tests/params/param-use.js +39 -0
- package/tests/tests/params/param.js +68 -0
- package/tests/tests/params/params-regex.js +26 -0
- package/tests/tests/params/params-use.js +20 -0
- package/tests/tests/params/params.js +35 -0
- package/tests/tests/req/req-accepts-charsets.js +40 -0
- package/tests/tests/req/req-accepts-encodings.js +36 -0
- package/tests/tests/req/req-accepts-languages.js +41 -0
- package/tests/tests/req/req-accepts.js +41 -0
- package/tests/tests/req/req-app.js +17 -0
- package/tests/tests/req/req-baseurl.js +38 -0
- package/tests/tests/req/req-connection.js +19 -0
- package/tests/tests/req/req-fresh.js +59 -0
- package/tests/tests/req/req-get.js +78 -0
- package/tests/tests/req/req-headers-distinct.js +72 -0
- package/tests/tests/req/req-headers.js +72 -0
- package/tests/tests/req/req-host.js +45 -0
- package/tests/tests/req/req-hostname.js +45 -0
- package/tests/tests/req/req-ip.js +19 -0
- package/tests/tests/req/req-is.js +44 -0
- package/tests/tests/req/req-original-url.js +29 -0
- package/tests/tests/req/req-param.js +29 -0
- package/tests/tests/req/req-protocol.js +20 -0
- package/tests/tests/req/req-query.js +23 -0
- package/tests/tests/req/req-range.js +48 -0
- package/tests/tests/req/req-raw-headers.js +72 -0
- package/tests/tests/req/req-subdomains.js +48 -0
- package/tests/tests/req/req-url-nested.js +27 -0
- package/tests/tests/req/req-url-optimized-router.js +26 -0
- package/tests/tests/req/req-url-optimized.js +23 -0
- package/tests/tests/req/req-url.js +36 -0
- package/tests/tests/req/req-xhr.js +23 -0
- package/tests/tests/res/head-content-length.js +18 -0
- package/tests/tests/res/head.js +47 -0
- package/tests/tests/res/injecting.js +25 -0
- package/tests/tests/res/piping.js +23 -0
- package/tests/tests/res/res-app.js +17 -0
- package/tests/tests/res/res-append.js +24 -0
- package/tests/tests/res/res-attachment.js +19 -0
- package/tests/tests/res/res-clear-cookie.js +18 -0
- package/tests/tests/res/res-cookie.js +22 -0
- package/tests/tests/res/res-download.js +36 -0
- package/tests/tests/res/res-format.js +57 -0
- package/tests/tests/res/res-get.js +18 -0
- package/tests/tests/res/res-headers-sent.js +18 -0
- package/tests/tests/res/res-json.js +17 -0
- package/tests/tests/res/res-jsonp.js +25 -0
- package/tests/tests/res/res-links.js +21 -0
- package/tests/tests/res/res-location.js +34 -0
- package/tests/tests/res/res-redirect.js +46 -0
- package/tests/tests/res/res-remove-header.js +19 -0
- package/tests/tests/res/res-send-file.js +17 -0
- package/tests/tests/res/res-send-status.js +17 -0
- package/tests/tests/res/res-send.js +69 -0
- package/tests/tests/res/res-set.js +28 -0
- package/tests/tests/res/res-status.js +18 -0
- package/tests/tests/res/res-type.js +19 -0
- package/tests/tests/res/res-vary.js +19 -0
- package/tests/tests/res/res-write.js +29 -0
- package/tests/tests/routers/complex-routers.js +34 -0
- package/tests/tests/routers/empty-router.js +25 -0
- package/tests/tests/routers/lot-of-routes.js +38 -0
- package/tests/tests/routers/mergeparams.js +42 -0
- package/tests/tests/routers/nested-routers.js +52 -0
- package/tests/tests/routers/router-options.js +68 -0
- package/tests/tests/routers/routers.js +45 -0
- package/tests/tests/routers/simple-routers.js +35 -0
- package/tests/tests/routing/all.js +47 -0
- package/tests/tests/routing/array-arguments.js +35 -0
- package/tests/tests/routing/array-use.js +33 -0
- package/tests/tests/routing/async-use.js +25 -0
- package/tests/tests/routing/complex-routes.js +50 -0
- package/tests/tests/routing/lot-of-param-routes.js +26 -0
- package/tests/tests/routing/lot-of-routes.js +59 -0
- package/tests/tests/routing/next-existent-optimized-route.js +29 -0
- package/tests/tests/routing/next-existent-route.js +29 -0
- package/tests/tests/routing/next-nonexistent-optimized-route.js +19 -0
- package/tests/tests/routing/next-nonexistent-route.js +19 -0
- package/tests/tests/routing/next-special-cases.js +54 -0
- package/tests/tests/routing/next-unoptimized.js +39 -0
- package/tests/tests/routing/no-path-use.js +29 -0
- package/tests/tests/routing/non-string-routes.js +27 -0
- package/tests/tests/routing/req-multiple-mountpaths.js +34 -0
- package/tests/tests/routing/simple-routes.js +29 -0
- package/tests/tests/routing/simple-use.js +52 -0
- package/tests/tests/routing/some-middlewares.js +27 -0
- package/tests/tests/routing/special-characters.js +28 -0
- package/tests/tests/routing/star.js +31 -0
- package/tests/tests/routing/sub-apps.js +32 -0
- package/tests/tests/routing/trailing-slash.js +37 -0
- package/tests/tests/routing/weird-route-start.js +18 -0
- package/tests/tests/send-file/accept-ranges.js +26 -0
- package/tests/tests/send-file/callback.js +23 -0
- package/tests/tests/send-file/default-error-routing.js +21 -0
- package/tests/tests/send-file/dotfiles.js +24 -0
- package/tests/tests/send-file/etag.js +19 -0
- package/tests/tests/send-file/fs-threads.js +39 -0
- package/tests/tests/send-file/head.js +49 -0
- package/tests/tests/send-file/headers.js +23 -0
- package/tests/tests/send-file/if-match.js +31 -0
- package/tests/tests/send-file/if-range.js +37 -0
- package/tests/tests/send-file/if-unmodified-since.js +32 -0
- package/tests/tests/send-file/immutable.js +22 -0
- package/tests/tests/send-file/large-file.js +19 -0
- package/tests/tests/send-file/last-modified.js +21 -0
- package/tests/tests/send-file/max-age.js +21 -0
- package/tests/tests/send-file/path-traversal.js +35 -0
- package/tests/tests/send-file/range.js +57 -0
- package/tests/tests/send-file/simple.js +18 -0
- package/tests/tests/settings/case-sensitive-routing.js +48 -0
- package/tests/tests/settings/env-errors.js +38 -0
- package/tests/tests/settings/etag.js +36 -0
- package/tests/tests/settings/json-escape.js +45 -0
- package/tests/tests/settings/json-replacer.js +50 -0
- package/tests/tests/settings/json-spaces.js +44 -0
- package/tests/tests/settings/query-parser.js +45 -0
- package/tests/tests/settings/strict-routing.js +64 -0
- package/tests/tests/settings/subdomain-offset.js +38 -0
- package/tests/tests/settings/trust-proxy-host.js +58 -0
- package/tests/tests/settings/trust-proxy-ip.js +58 -0
- package/tests/tests/settings/trust-proxy-ips.js +58 -0
- package/tests/tests/settings/trust-proxy-protocol.js +46 -0
- package/tests/tests/settings/x-powered-by.js +32 -0
- package/tests/uws.js +14 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// must support cookie parser
|
|
2
|
+
|
|
3
|
+
const express = require("express");
|
|
4
|
+
const cookieParser = require("cookie-parser");
|
|
5
|
+
|
|
6
|
+
const app = express();
|
|
7
|
+
|
|
8
|
+
app.use(cookieParser());
|
|
9
|
+
|
|
10
|
+
app.get('/abc', (req, res) => {
|
|
11
|
+
console.log(req.cookies);
|
|
12
|
+
res.send('ok');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
app.listen(13333, async () => {
|
|
16
|
+
console.log('Server is running on port 13333');
|
|
17
|
+
|
|
18
|
+
await Promise.all([
|
|
19
|
+
fetch('http://localhost:13333/abc', {
|
|
20
|
+
headers: {
|
|
21
|
+
'Cookie': 'abc=123; def=456'
|
|
22
|
+
}
|
|
23
|
+
}).then(res => res.text())
|
|
24
|
+
]);
|
|
25
|
+
|
|
26
|
+
process.exit(0);
|
|
27
|
+
|
|
28
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// must support cookie-session middleware
|
|
2
|
+
|
|
3
|
+
const express = require("express");
|
|
4
|
+
const cookieSession = require('cookie-session');
|
|
5
|
+
|
|
6
|
+
const app = express();
|
|
7
|
+
|
|
8
|
+
app.use(cookieSession({
|
|
9
|
+
name: 'session',
|
|
10
|
+
keys: ["test"],
|
|
11
|
+
|
|
12
|
+
// Cookie Options
|
|
13
|
+
maxAge: 24 * 60 * 60 * 1000 // 24 hours
|
|
14
|
+
}));
|
|
15
|
+
|
|
16
|
+
app.get('/abc', (req, res) => {
|
|
17
|
+
req.session.views = (req.session.views || 0) + 1;
|
|
18
|
+
res.status(202).send(`Viewed ${req.session.views} times.`);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
app.listen(13333, async () => {
|
|
22
|
+
console.log('Server is running on port 13333');
|
|
23
|
+
|
|
24
|
+
const response = await fetch('http://localhost:13333/abc');
|
|
25
|
+
const sessionCookie = response.headers.get('Set-Cookie').match(/session=(.*?);/)[1];
|
|
26
|
+
const sessionSig = response.headers.get('Set-Cookie').match(/session.sig=(.*?);/)[1];
|
|
27
|
+
const text = await response.text();
|
|
28
|
+
console.log(text, sessionCookie, sessionSig, response.status);
|
|
29
|
+
|
|
30
|
+
const response2 = await fetch('http://localhost:13333/abc', {
|
|
31
|
+
headers: {
|
|
32
|
+
'Cookie': `session=${sessionCookie}; session.sig=${sessionSig}`
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
const text2 = await response2.text();
|
|
36
|
+
console.log(text2);
|
|
37
|
+
|
|
38
|
+
process.exit(0);
|
|
39
|
+
|
|
40
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// must support cors middleware
|
|
2
|
+
|
|
3
|
+
const express = require("express");
|
|
4
|
+
const cors = require("cors");
|
|
5
|
+
|
|
6
|
+
const app = express();
|
|
7
|
+
|
|
8
|
+
app.get('/abc', cors(), (req, res) => {
|
|
9
|
+
res.send('1');
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
app.get('/def', cors({
|
|
13
|
+
origin: 'http://example.com',
|
|
14
|
+
}), (req, res) => {
|
|
15
|
+
res.send('2');
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
app.listen(13333, async () => {
|
|
19
|
+
console.log('Server is running on port 13333');
|
|
20
|
+
|
|
21
|
+
const response = await fetch('http://localhost:13333/abc');
|
|
22
|
+
console.log(response.headers.get('access-control-allow-origin'));
|
|
23
|
+
|
|
24
|
+
const response2 = await fetch('http://localhost:13333/def');
|
|
25
|
+
console.log(response2.headers.get('access-control-allow-origin'));
|
|
26
|
+
|
|
27
|
+
process.exit(0);
|
|
28
|
+
|
|
29
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// must support errorhandler middleware
|
|
2
|
+
|
|
3
|
+
const express = require("express");
|
|
4
|
+
const errorhandler = require('errorhandler')
|
|
5
|
+
|
|
6
|
+
const app = express();
|
|
7
|
+
|
|
8
|
+
app.get('/abc', (req, res) => {
|
|
9
|
+
throw new Error('test');
|
|
10
|
+
res.send(req.body);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
app.use(errorhandler({
|
|
14
|
+
log: (err) => {
|
|
15
|
+
console.log(`hi`, err.message);
|
|
16
|
+
}
|
|
17
|
+
}));
|
|
18
|
+
|
|
19
|
+
app.listen(13333, async () => {
|
|
20
|
+
console.log('Server is running on port 13333');
|
|
21
|
+
|
|
22
|
+
const response = await fetch('http://localhost:13333/abc');
|
|
23
|
+
|
|
24
|
+
process.exit(0);
|
|
25
|
+
|
|
26
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// must support express-fileupload middleware with temp file
|
|
2
|
+
|
|
3
|
+
const express = require("express");
|
|
4
|
+
const fileUpload = require("express-fileupload");
|
|
5
|
+
const fs = require("fs");
|
|
6
|
+
|
|
7
|
+
const app = express();
|
|
8
|
+
|
|
9
|
+
app.post('/file', fileUpload({
|
|
10
|
+
tempFileDir: './tmp',
|
|
11
|
+
useTempFiles: true,
|
|
12
|
+
debug: false
|
|
13
|
+
}), (req, res) => {
|
|
14
|
+
req.files.file.mv('./tmp/test.txt');
|
|
15
|
+
res.send(req.files.file.size.toString());
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
app.listen(13333, async () => {
|
|
19
|
+
console.log('Server is running on port 13333');
|
|
20
|
+
|
|
21
|
+
const formData = new FormData();
|
|
22
|
+
console.log('creating file');
|
|
23
|
+
const arr = new Uint8Array(1024 * 256); // 256 KB
|
|
24
|
+
console.log('filling file');
|
|
25
|
+
for (let i = 0; i < arr.length; i++) {
|
|
26
|
+
arr[i] = i % 256;
|
|
27
|
+
}
|
|
28
|
+
console.log('appending file');
|
|
29
|
+
const file = new File(arr, 'test.txt');
|
|
30
|
+
formData.append('file', file);
|
|
31
|
+
|
|
32
|
+
console.log('sending request');
|
|
33
|
+
const response = await fetch('http://localhost:13333/file', {
|
|
34
|
+
method: 'POST',
|
|
35
|
+
body: formData
|
|
36
|
+
});
|
|
37
|
+
console.log('response', response.status);
|
|
38
|
+
const text = await response.text();
|
|
39
|
+
console.log(text);
|
|
40
|
+
|
|
41
|
+
console.log('temp file', fs.statSync('./tmp/test.txt').size);
|
|
42
|
+
fs.rmSync('./tmp', { recursive: true, force: true });
|
|
43
|
+
|
|
44
|
+
process.exit(0);
|
|
45
|
+
|
|
46
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// must support express-fileupload middleware
|
|
2
|
+
|
|
3
|
+
const express = require("express");
|
|
4
|
+
const fileUpload = require("express-fileupload");
|
|
5
|
+
|
|
6
|
+
const app = express();
|
|
7
|
+
|
|
8
|
+
app.post('/file', fileUpload(), (req, res) => {
|
|
9
|
+
res.send(req.files);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
app.listen(13333, async () => {
|
|
13
|
+
console.log('Server is running on port 13333');
|
|
14
|
+
|
|
15
|
+
const formData = new FormData();
|
|
16
|
+
const file = new File([1, 2, 3], 'test.txt');
|
|
17
|
+
formData.append('file', file);
|
|
18
|
+
|
|
19
|
+
const response = await fetch('http://localhost:13333/file', {
|
|
20
|
+
method: 'POST',
|
|
21
|
+
body: formData
|
|
22
|
+
});
|
|
23
|
+
const text = await response.text();
|
|
24
|
+
console.log(text);
|
|
25
|
+
|
|
26
|
+
process.exit(0);
|
|
27
|
+
|
|
28
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// must support express-rate-limit middleware
|
|
2
|
+
|
|
3
|
+
const express = require("express");
|
|
4
|
+
const { rateLimit } = require('express-rate-limit');
|
|
5
|
+
|
|
6
|
+
const app = express();
|
|
7
|
+
|
|
8
|
+
app.use(rateLimit({
|
|
9
|
+
windowMs: 15 * 60 * 1000, // 15 minutes
|
|
10
|
+
limit: 1,
|
|
11
|
+
legacyHeaders: true,
|
|
12
|
+
handler: (req, res, next) => {
|
|
13
|
+
console.log('Too Many Requests called');
|
|
14
|
+
res.status(429).send('Too Many Requests');
|
|
15
|
+
}
|
|
16
|
+
}));
|
|
17
|
+
|
|
18
|
+
app.get('/abc', (req, res) => {
|
|
19
|
+
res.send('Hello World');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
app.listen(13333, async () => {
|
|
23
|
+
console.log('Server is running on port 13333');
|
|
24
|
+
|
|
25
|
+
const response = await fetch('http://localhost:13333/abc');
|
|
26
|
+
console.log(await response.text());
|
|
27
|
+
|
|
28
|
+
const response2 = await fetch('http://localhost:13333/abc');
|
|
29
|
+
console.log(await response2.text(), response2.headers.get('X-RateLimit-Limit'), response2.headers.get('X-RateLimit-Remaining'));
|
|
30
|
+
|
|
31
|
+
process.exit(0);
|
|
32
|
+
|
|
33
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// must support express-session middleware
|
|
2
|
+
|
|
3
|
+
const express = require("express");
|
|
4
|
+
const session = require('express-session');
|
|
5
|
+
|
|
6
|
+
const app = express();
|
|
7
|
+
|
|
8
|
+
app.use(session({
|
|
9
|
+
secret: 'keyboard cat',
|
|
10
|
+
resave: false,
|
|
11
|
+
saveUninitialized: true
|
|
12
|
+
}));
|
|
13
|
+
|
|
14
|
+
app.get('/abc', (req, res) => {
|
|
15
|
+
req.session.views = (req.session.views || 0) + 1;
|
|
16
|
+
res.status(202).send(`Viewed ${req.session.views} times.`);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
app.listen(13333, async () => {
|
|
20
|
+
console.log('Server is running on port 13333');
|
|
21
|
+
|
|
22
|
+
const response = await fetch('http://localhost:13333/abc');
|
|
23
|
+
const cookie = response.headers.get('Set-Cookie').match(/connect.sid=(.*?);/)[1];
|
|
24
|
+
const text = await response.text();
|
|
25
|
+
console.log(text, response.status);
|
|
26
|
+
|
|
27
|
+
const response2 = await fetch('http://localhost:13333/abc', {
|
|
28
|
+
headers: {
|
|
29
|
+
'Cookie': `connect.sid=${cookie}`
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
const text2 = await response2.text();
|
|
33
|
+
console.log(text2);
|
|
34
|
+
|
|
35
|
+
process.exit(0);
|
|
36
|
+
|
|
37
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// must support express.static() options
|
|
2
|
+
|
|
3
|
+
const express = require("express");
|
|
4
|
+
|
|
5
|
+
const app = express();
|
|
6
|
+
|
|
7
|
+
app.post('/abc', (req, res) => {
|
|
8
|
+
res.send('ok');
|
|
9
|
+
});
|
|
10
|
+
app.use('/static', express.static('tests/parts'));
|
|
11
|
+
app.use('/static2', express.static('tests/parts', { redirect: false }));
|
|
12
|
+
app.use('/static3', express.static('tests/parts', { index: 'index.pug' }));
|
|
13
|
+
app.use('/static4', express.static('tests/parts', { dotfiles: 'allow' }));
|
|
14
|
+
app.use('/static5', express.static('tests/parts', { dotfiles: 'deny' }));
|
|
15
|
+
app.use('/static6', express.static('tests/parts', { dotfiles: 'ignore' }));
|
|
16
|
+
app.use('/static7', express.static('tests/parts', { fallthrough: false }));
|
|
17
|
+
app.use('/static8', express.static('tests/parts', { etag: false }));
|
|
18
|
+
app.use('/static9', express.static('tests/parts', { extensions: ['jpg'] }));
|
|
19
|
+
app.use('/static10', express.static('tests/parts', { setHeaders: (res, path, stat) => {
|
|
20
|
+
console.log(path, stat.size);
|
|
21
|
+
res.set('X-Test', 'test');
|
|
22
|
+
} }));
|
|
23
|
+
|
|
24
|
+
app.use((err, req, res, next) => {
|
|
25
|
+
|
|
26
|
+
res.status(500).send(err);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
app.use((req, res, next) => {
|
|
30
|
+
|
|
31
|
+
res.status(404).send('404');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
app.listen(13333, async () => {
|
|
35
|
+
console.log('Server is running on port 13333');
|
|
36
|
+
|
|
37
|
+
const response = await fetch('http://localhost:13333/static', { redirect: 'manual' });
|
|
38
|
+
console.log(response.status, response.headers.get('Location'));
|
|
39
|
+
|
|
40
|
+
const response2 = await fetch('http://localhost:13333/static2', { redirect: 'manual' });
|
|
41
|
+
console.log(response2.status, response2.headers.get('Location'));
|
|
42
|
+
|
|
43
|
+
const response3 = await fetch('http://localhost:13333/static3');
|
|
44
|
+
console.log(response3.status);
|
|
45
|
+
|
|
46
|
+
const response4 = await fetch('http://localhost:13333/static4/.test/index.html');
|
|
47
|
+
console.log(response4.status, (await response4.text()).slice(0, 100));
|
|
48
|
+
|
|
49
|
+
const response5 = await fetch('http://localhost:13333/static5/.test/index.html');
|
|
50
|
+
console.log(response5.status, await response5.text());
|
|
51
|
+
|
|
52
|
+
const response6 = await fetch('http://localhost:13333/static6/.test/index.html');
|
|
53
|
+
console.log(response6.status, await response6.text());
|
|
54
|
+
|
|
55
|
+
const response7 = await fetch('http://localhost:13333/static7/assgdgeerdf.html');
|
|
56
|
+
console.log(response7.status, (await response7.text()).includes('ENOENT'));
|
|
57
|
+
|
|
58
|
+
const response8 = await fetch('http://localhost:13333/static8/index.html');
|
|
59
|
+
console.log(response8.headers.get('ETag'));
|
|
60
|
+
|
|
61
|
+
const response9 = await fetch('http://localhost:13333/static/index.html');
|
|
62
|
+
console.log(response9.headers.get('ETag'));
|
|
63
|
+
|
|
64
|
+
const response10 = await fetch('http://localhost:13333/static9/big');
|
|
65
|
+
console.log(response10.status, response10.headers.get('Content-Type'));
|
|
66
|
+
|
|
67
|
+
const response11 = await fetch('http://localhost:13333/static10/index.html');
|
|
68
|
+
console.log(response11.headers.get('X-Test'));
|
|
69
|
+
|
|
70
|
+
process.exit(0);
|
|
71
|
+
|
|
72
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// must support express.static()
|
|
2
|
+
|
|
3
|
+
const express = require("express");
|
|
4
|
+
|
|
5
|
+
const app = express();
|
|
6
|
+
|
|
7
|
+
app.post('/abc', (req, res) => {
|
|
8
|
+
res.send('ok');
|
|
9
|
+
});
|
|
10
|
+
app.use('/static', (req, res, next) => {
|
|
11
|
+
express.static('src')(req, res, e => {
|
|
12
|
+
console.log('caught', e);
|
|
13
|
+
next();
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
app.use('/static', express.static('tests/parts'));
|
|
18
|
+
|
|
19
|
+
app.use((req, res, next) => {
|
|
20
|
+
res.status(404).send('404');
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
app.listen(13333, async () => {
|
|
24
|
+
console.log('Server is running on port 13333');
|
|
25
|
+
|
|
26
|
+
const responses = await Promise.all([
|
|
27
|
+
fetch('http://localhost:13333/abc'),
|
|
28
|
+
fetch('http://localhost:13333/static/workers'),
|
|
29
|
+
fetch('http://localhost:13333/static/'),
|
|
30
|
+
fetch('http://localhost:13333/static/index.js?1'),
|
|
31
|
+
fetch('http://localhost:13333/static/../package.json'),
|
|
32
|
+
]);
|
|
33
|
+
|
|
34
|
+
const texts = await Promise.all(responses.map(r => r.text()));
|
|
35
|
+
|
|
36
|
+
console.log(texts.map(t => t.slice(0, 30)));
|
|
37
|
+
|
|
38
|
+
process.exit(0);
|
|
39
|
+
|
|
40
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// must support method-override middleware
|
|
2
|
+
|
|
3
|
+
const express = require("express");
|
|
4
|
+
const methodOverride = require("method-override");
|
|
5
|
+
|
|
6
|
+
const app = express();
|
|
7
|
+
|
|
8
|
+
app.use(methodOverride('X-HTTP-Method-Override'));
|
|
9
|
+
|
|
10
|
+
app.post('/abc', (req, res) => {
|
|
11
|
+
res.send(req.method+'1');
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
app.use((req, res, next) => {
|
|
15
|
+
res.send(req.method+'2');
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
app.listen(13333, async () => {
|
|
19
|
+
console.log('Server is running on port 13333');
|
|
20
|
+
|
|
21
|
+
const response = await fetch('http://localhost:13333/abc', {
|
|
22
|
+
method: 'GET',
|
|
23
|
+
headers: {
|
|
24
|
+
'X-HTTP-Method-Override': 'POST',
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const text = await response.text();
|
|
29
|
+
console.log(text);
|
|
30
|
+
|
|
31
|
+
process.exit(0);
|
|
32
|
+
|
|
33
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// must support multer middleware
|
|
2
|
+
|
|
3
|
+
const express = require("express");
|
|
4
|
+
const multer = require("multer");
|
|
5
|
+
|
|
6
|
+
const app = express();
|
|
7
|
+
const upload = multer();
|
|
8
|
+
|
|
9
|
+
app.post('/abc', upload.none(), (req, res) => {
|
|
10
|
+
res.send(req.body);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
app.post('/file', upload.single('file'), (req, res) => {
|
|
14
|
+
res.send(req.file);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
app.listen(13333, async () => {
|
|
18
|
+
console.log('Server is running on port 13333');
|
|
19
|
+
|
|
20
|
+
const formData = new FormData();
|
|
21
|
+
formData.append('abc', '123');
|
|
22
|
+
|
|
23
|
+
const response = await fetch('http://localhost:13333/abc', {
|
|
24
|
+
method: 'POST',
|
|
25
|
+
body: formData
|
|
26
|
+
});
|
|
27
|
+
const text = await response.text();
|
|
28
|
+
console.log(text);
|
|
29
|
+
|
|
30
|
+
const formData2 = new FormData();
|
|
31
|
+
const file = new File([1, 2, 3], 'test.txt');
|
|
32
|
+
formData2.append('file', file);
|
|
33
|
+
|
|
34
|
+
const response2 = await fetch('http://localhost:13333/file', {
|
|
35
|
+
method: 'POST',
|
|
36
|
+
body: formData2
|
|
37
|
+
});
|
|
38
|
+
const text2 = await response2.text();
|
|
39
|
+
console.log(text2);
|
|
40
|
+
|
|
41
|
+
process.exit(0);
|
|
42
|
+
|
|
43
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// must support multiple middlewares
|
|
2
|
+
|
|
3
|
+
const express = require("express");
|
|
4
|
+
const bodyParser = require("body-parser");
|
|
5
|
+
const fileUpload = require("express-fileupload");
|
|
6
|
+
|
|
7
|
+
const app = express();
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
app.post('/abc', bodyParser.raw(), fileUpload(), (req, res) => {
|
|
11
|
+
console.log(req.body, req.files);
|
|
12
|
+
res.send('ok');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
app.listen(13333, async () => {
|
|
16
|
+
console.log('Server is running on port 13333');
|
|
17
|
+
|
|
18
|
+
const formData = new FormData();
|
|
19
|
+
console.log('creating file');
|
|
20
|
+
const arr = [...new Array(1024 * 32)].map((_, i) => i % 256); // 32 KB
|
|
21
|
+
const file = new File(arr, 'test.txt');
|
|
22
|
+
console.log('appending file');
|
|
23
|
+
formData.append('file', file);
|
|
24
|
+
formData.append('text', 'hello');
|
|
25
|
+
console.log('sending request');
|
|
26
|
+
|
|
27
|
+
const response = await fetch('http://localhost:13333/abc', {
|
|
28
|
+
method: 'POST',
|
|
29
|
+
body: formData
|
|
30
|
+
});
|
|
31
|
+
console.log('reading response');
|
|
32
|
+
const text = await response.text();
|
|
33
|
+
console.log(text);
|
|
34
|
+
|
|
35
|
+
process.exit(0);
|
|
36
|
+
|
|
37
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// must support response-time middleware
|
|
2
|
+
|
|
3
|
+
const express = require("express");
|
|
4
|
+
const responseTime = require("response-time");
|
|
5
|
+
|
|
6
|
+
const app = express();
|
|
7
|
+
|
|
8
|
+
app.use(responseTime((req, res, time) => {
|
|
9
|
+
console.log(typeof time);
|
|
10
|
+
}));
|
|
11
|
+
|
|
12
|
+
app.post('/abc', (req, res) => {
|
|
13
|
+
res.send('1');
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
app.listen(13333, async () => {
|
|
17
|
+
console.log('Server is running on port 13333');
|
|
18
|
+
|
|
19
|
+
const response = await fetch('http://localhost:13333/abc', {
|
|
20
|
+
method: 'POST',
|
|
21
|
+
body: 'abc',
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const text = await response.text();
|
|
25
|
+
console.log(text);
|
|
26
|
+
|
|
27
|
+
process.exit(0);
|
|
28
|
+
|
|
29
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// must support serve index
|
|
2
|
+
|
|
3
|
+
const express = require("express");
|
|
4
|
+
const serveStatic = require("serve-static");
|
|
5
|
+
const serveIndex = require("serve-index");
|
|
6
|
+
|
|
7
|
+
const app = express();
|
|
8
|
+
|
|
9
|
+
app.use('/static', serveStatic('src'), (req, res, next) => {
|
|
10
|
+
serveIndex('src')(req, res, next);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
app.get('/static/asdf', (req, res, next) => {
|
|
14
|
+
res.send(req.url);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
app.use((req, res, next) => {
|
|
18
|
+
res.status(404).send('404');
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
app.listen(13333, async () => {
|
|
22
|
+
console.log('Server is running on port 13333');
|
|
23
|
+
|
|
24
|
+
const responses = await Promise.all([
|
|
25
|
+
fetch('http://localhost:13333/abc'),
|
|
26
|
+
fetch('http://localhost:13333/static'),
|
|
27
|
+
fetch('http://localhost:13333/static/workers'),
|
|
28
|
+
fetch('http://localhost:13333/static/index.js'),
|
|
29
|
+
fetch('http://localhost:13333/static/../package.json'),
|
|
30
|
+
]);
|
|
31
|
+
|
|
32
|
+
const texts = await Promise.all(responses.map(r => r.text()));
|
|
33
|
+
|
|
34
|
+
console.log(texts.map(i => i.slice(0, 100)));
|
|
35
|
+
|
|
36
|
+
process.exit(0);
|
|
37
|
+
|
|
38
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// must support serve static
|
|
2
|
+
|
|
3
|
+
const express = require("express");
|
|
4
|
+
const serveStatic = require("serve-static");
|
|
5
|
+
|
|
6
|
+
const app = express();
|
|
7
|
+
|
|
8
|
+
app.post('/abc', (req, res) => {
|
|
9
|
+
res.send('ok');
|
|
10
|
+
});
|
|
11
|
+
app.use('/static', (req, res, next) => {
|
|
12
|
+
serveStatic('src')(req, res, () => {
|
|
13
|
+
console.log('caught');
|
|
14
|
+
next();
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
app.use((req, res, next) => {
|
|
19
|
+
res.status(404).send('404');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
app.listen(13333, async () => {
|
|
23
|
+
console.log('Server is running on port 13333');
|
|
24
|
+
|
|
25
|
+
const responses = await Promise.all([
|
|
26
|
+
fetch('http://localhost:13333/abc'),
|
|
27
|
+
fetch('http://localhost:13333/static/workers'),
|
|
28
|
+
fetch('http://localhost:13333/static/index.js'),
|
|
29
|
+
fetch('http://localhost:13333/static/../package.json'),
|
|
30
|
+
]);
|
|
31
|
+
|
|
32
|
+
const texts = await Promise.all(responses.map(r => r.text()));
|
|
33
|
+
|
|
34
|
+
console.log(texts);
|
|
35
|
+
|
|
36
|
+
process.exit(0);
|
|
37
|
+
|
|
38
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// must support vhost middleware
|
|
2
|
+
|
|
3
|
+
const net = require("net");
|
|
4
|
+
|
|
5
|
+
async function sendRequest(method, url, customHost) {
|
|
6
|
+
// arrayHeaders is an array of [key, value] pairs
|
|
7
|
+
return new Promise((resolve, reject) => {
|
|
8
|
+
const client = new net.Socket();
|
|
9
|
+
const [host, port] = url.split('://')[1].split('/')[0].split(':');
|
|
10
|
+
const path = '/' + url.split('/').slice(3).join('/');
|
|
11
|
+
|
|
12
|
+
client.connect(parseInt(port), host, () => {
|
|
13
|
+
let request = `${method} ${path} HTTP/1.1\r\n`;
|
|
14
|
+
request += `Host: ${customHost}\r\n`;
|
|
15
|
+
|
|
16
|
+
request += '\r\n';
|
|
17
|
+
|
|
18
|
+
client.on('data', (data) => {
|
|
19
|
+
resolve(data.toString().split('\r\n\r\n')[1]);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
client.write(request);
|
|
23
|
+
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const express = require("express");
|
|
29
|
+
const vhost = require("vhost");
|
|
30
|
+
|
|
31
|
+
const app = express();
|
|
32
|
+
|
|
33
|
+
app.use(vhost('*.example.com', (req, res) => {
|
|
34
|
+
res.send('Hello from example.com');
|
|
35
|
+
}));
|
|
36
|
+
|
|
37
|
+
app.get('/abc', (req, res) => {
|
|
38
|
+
res.send('hi');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
app.listen(13333, async () => {
|
|
42
|
+
console.log('Server is running on port 13333');
|
|
43
|
+
|
|
44
|
+
console.log(await sendRequest('GET', 'http://localhost:13333/abc', 'example.com'));
|
|
45
|
+
console.log(await sendRequest('GET', 'http://localhost:13333/abc', 'sub.example.com'));
|
|
46
|
+
console.log(await sendRequest('GET', 'http://localhost:13333/abc', 'localhost.com'));
|
|
47
|
+
|
|
48
|
+
process.exit(0);
|
|
49
|
+
|
|
50
|
+
});
|