Nginx 配置
使用 Nginx ,配置如下:
location /hello {
rewrite ^/hello$ / break;
rewrite ^/hello(.*)/$ /hello$1 permanent; # Remove trailing /
rewrite ^/hello/(.*) /$1 break;
proxy_pass http://127.0.0.1:4055;
proxy_set_header X-Real-IP $remote_addr;
}
创建并运行服务器
创建工作目录,并初始化:
mkdir hello
cd hello
npm init
安装 Express.js
npm install express
创建文件 index.js,内容如下:
const express = require('express')
const app = express()
const port = 4055
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
运行服务器:
node index.js
Done. 用浏览器访问服务器: https://localhost/hello, 就可以看到输出显示:Hello World!
官方测试站点测试结果
上面的例子来自 Express 官方文档(https://expressjs.com/en/starter/hello-world.html)。官方文档还提供了测试服务器,测试结果如下:
17:20:50.306
Request GET / ip=123.121.30.143id=899b5bd41f3c
HEADERS
accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
accept-encoding:gzip, deflate, br
accept-language:en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,zh-TW;q=0.6,fr;q=0.5
connection:close
host:2h1qhghhf4v8.runkit.sh
referer:https://runkit.com/e?name=runkit-embed-0&preamble=&location=https%3A%2F%2Fexpressjs.com%2Fen%2Fstarter%2Fhello-world.html&readOnly=false&mode=endpoint&minHeight=&theme=&syntaxTheme=&gutterStyle=outside&usedDeprecatedOptions=%5B%5D&base64source=Y29uc3QgZXhwcmVzcyA9IHJlcXVpcmUoJ2V4cHJlc3MnKQpjb25zdCBhcHAgPSBleHByZXNzKCkKY29uc3QgcG9ydCA9IDMwMDAKCmFwcC5nZXQoCkp&
runkit-rate-limit-remaining:7500
runkit-request-id:e03c70ab-91b3-4f81-a3b7-899b5bd41f3c
sec-fetch-mode:navigate
sec-fetch-site:cross-site
sec-fetch-user:?1
upgrade-insecure-requests:1
user-agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36
x-forwarded-proto:https
17:20:50.319
Response GET / status=200duration=id=899b5bd41f3c
HEADERS
connection:close
content-length:12
content-type:text/html; charset=utf-8
date:Sat, 14 Sep 2019 09:20:50 GMT
etag:W/"c-Lve95gjOVATpfV8EL5X4nxwjKHE"
runkit-rate-limit-remaining:7500
x-powered-by:runkit.com
17:20:51.376
Request GET /favicon.ico ip=123.121.30.143id=8d5b290b0b97
HEADERS
accept:image/webp,image/apng,image/*,*/*;q=0.8
accept-encoding:gzip, deflate, br
accept-language:en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,zh-TW;q=0.6,fr;q=0.5
connection:close
host:2h1qhghhf4v8.runkit.sh
referer:https://2h1qhghhf4v8.runkit.sh/
runkit-rate-limit-remaining:7499
runkit-request-id:016ebf7d-ec8c-46ce-8009-8d5b290b0b97
sec-fetch-mode:no-cors
sec-fetch-site:same-origin
user-agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36
x-forwarded-proto:https
17:20:51.384
Response GET /favicon.ico status=404duration=id=8d5b290b0b97
HEADERS
connection:close
content-length:150
content-security-policy:default-src 'none'
content-type:text/html; charset=utf-8
date:Sat, 14 Sep 2019 09:20:51 GMT
runkit-rate-limit-remaining:7499
x-content-type-options:nosniff
x-powered-by:runkit.com