目錄
neo@MacBook-Pro ~ % mkdir node neo@MacBook-Pro ~ % cd node neo@MacBook-Pro ~/node % npm install express --save neo@MacBook-Pro ~/node % npm list express /Users/neo/node └── express@4.16.3
npm install body-parser --save npm install cookie-parser --save npm install multer --save
echo 'node_modules' > .gitignore
Hello world 測試程序
neo@MacBook-Pro ~/node % cat demo.js var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello World'); }) var server = app.listen(8080, function () { var host = server.address().address var port = server.address().port console.log("應用實例,訪問地址為 http://%s:%s", host, port) })
neo@MacBook-Pro ~/node % node demo.js 應用實例,訪問地址為 http://:::8080
瀏覽器訪問 http://localhost:8080 將看到 Hello World 字元串