最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Mac中搭建本地NodeJS的web服务器

Mac crifan 1257浏览 0评论
折腾:
【记录】Mac中折腾Cesium
期间,需要在Mac中搭建一个本地的web server
mac nodejs web server
mac node js web server
Getting Started Guide | Node.js
Once we have installed Node.js, let’s build our first web server. Create a file named app.jscontaining the following contents:
所以需要先去安装
Node.js
node-v14.1.0.pkg
Setting up Node.js and npm on Mac OSX | George Ornbo
好像就是node?
那之前已安装过的
node: stable 13.12.0 (bottled), HEAD
Platform built on V8 to build network applications
https://nodejs.org/
/usr/local/Cellar/node/9.9.0 (5,125 files, 49.7MB) *
  Poured from bottle on 2018-04-17 at 10:52:40
From: https://mirrors.ustc.edu.cn/homebrew-core.git/Formula/node.rb
==> Dependencies
Build: pkg-config ✘, python ✘
Required: icu4c ✔
==> Options
--HEAD
    Install HEAD version
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d
看看版本:
 crifan@licrifandeMacBook-Pro  ~  node -v
v10.4.0
 crifan@licrifandeMacBook-Pro  ~  npm -v
6.1.0
Setting Up a Local Web Server using Node.js ← Sabe.io
npm install http-server -g
log
 crifan@licrifandeMacBook-Pro  ~  npm install http-server -g
/Users/crifan/.nvm/versions/node/v10.4.0/bin/http-server -> /Users/crifan/.nvm/versions/node/v10.4.0/lib/node_modules/http-server/bin/http-server
/Users/crifan/.nvm/versions/node/v10.4.0/bin/hs -> /Users/crifan/.nvm/versions/node/v10.4.0/lib/node_modules/http-server/bin/http-server
+ [email protected]
added 23 packages from 35 contributors in 8.218s
const http = require('http');


const hostname = '127.0.0.1';
const port = 3000;


const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});


server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
去启动试试
是可以的。
那再去把Cesium的文件,放到同目录?
好像也不对。

Introduction to Node.js
 crifan@licrifandeMacBook-Pro  ~  http-server --help
usage: http-server [path] [options]


options:
  -p --port    Port to use [8080]
  -a           Address to use [0.0.0.0]
  -d           Show directory listings [true]
  -i           Display autoIndex [true]
  -g --gzip    Serve gzip files when possible [false]
  -b --brotli  Serve brotli files when possible [false]
               If both brotli and gzip are enabled, brotli takes precedence
  -e --ext     Default file extension if none supplied [none]
  -s --silent  Suppress log messages from output
  --cors[=headers]   Enable CORS via the "Access-Control-Allow-Origin" header
                     Optionally provide CORS headers list separated by commas
  -o [path]    Open browser window after starting the server.
               Optionally provide a URL path to open the browser window to.
  -c           Cache time (max-age) in seconds [3600], e.g. -c10 for 10 seconds.
               To disable caching, use -c-1.
  -t           Connections timeout in seconds [120], e.g. -t60 for 1 minute.
               To disable timeout, use -t0
  -U --utc     Use UTC time format in log messages.
  --log-ip     Enable logging of the client's IP address


  -P --proxy         Fallback proxy if the request cannot be resolved. e.g.: http://someurl.com


  --username   Username for basic authentication [none]
               Can also be specified with the env variable NODE_HTTP_SERVER_USERNAME
  --password   Password for basic authentication [none]
               Can also be specified with the env variable NODE_HTTP_SERVER_PASSWORD


  -S --ssl     Enable https.
  -C --cert    Path to ssl cert file (default: cert.pem).
  -K --key     Path to ssl key file (default: key.pem).


  -r --robots        Respond to /robots.txt [User-agent: *\nDisallow: /]
  --no-dotfiles      Do not show dotfiles
  -h --help          Print this list and exit.
  -v --version       Print the version and exit.
然后去该目录下运行:
 ✘ crifan@licrifandeMacBook-Pro  ~/dev/dev_root/projects/cesiumTest   http-server -o
Starting up http-server, serving ./
Available on:
  http://127.0.0.1:8080
  http://192.168.31.12:8080
Hit CTRL-C to stop the server
open: http://127.0.0.1:8080
[Thu Apr 30 2020 21:44:08 GMT+0800 (GMT+08:00)]  "GET /" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.4 Safari/605.1.15"
[Thu Apr 30 2020 21:44:09 GMT+0800 (GMT+08:00)]  "GET /favicon.ico" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.4 Safari/605.1.15"
[Thu Apr 30 2020 21:44:09 GMT+0800 (GMT+08:00)]  "GET /favicon.ico" Error (404): "Not found"
自动打开页面:

转载请注明:在路上 » 【已解决】Mac中搭建本地NodeJS的web服务器

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.185 seconds, using 22.11MB memory