$ npm list express express_demo@1.0.0 /Users/morose/Documents/Temp/Example/Express_demo └── express@4.17.1
Express 框架範例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// express_demo.js 文件 var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello World'); }) var server = app.listen(8081, function () { var host = server.address().address var port = server.address().port console.log("瀏覽地址為 http://%s:%s", host, port) })
If port is omitted or is 0, the operating system will assign an arbitrary unused port, which can be retrieved by using server.address().port after the 'listening' event has been emitted.
If host is omitted, the server will accept connections on the unspecified IPv6 address (::) when IPv6 is available, or the unspecified IPv4 address (0.0.0.0) otherwise.
In most operating systems, listening to the unspecified IPv6 address (::) may cause the net.Server to also listen on the unspecified IPv4 address (0.0.0.0).
// express_demo.js 文件 var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello World'); }) var server = app.listen(8081, '0.0.0.0', function () { var host = server.address().address var port = server.address().port console.log("瀏覽地址為 http://%s:%s", host, port) })
// express_demo3.js 文件 var express = require('express'); var app = express(); app.use('/public', express.static('public')); app.get('/', function (req, res) { res.send('Hello World'); }) var server = app.listen(8081, '0.0.0.0', function () { var host = server.address().address var port = server.address().port console.log("瀏覽地址為 http://%s:%s", host, port) })