$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help json`for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
name: (my-app)
version: (1.0.0)
description:
entry point: (index.js)test command:
git repository:
keywords:
author:
license: (ISC)
About to write to E:\tmp\my-app\package.json:
{"name": "my-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {"test": "echo \"Error: no test specified\" && exit 1"},
"author": "",
"license": "ISC"}
Is this ok? (yes)
varexpress=require('express');varapp=express();app.listen(3000,function(){console.log('i am alive on port 3000');});app.use(express.static(__dirname+'/public'));
使用Node.js搭建一个简单的静态文件server。
介绍一下如何利用Node.js搭建一个简单的静态文件server。 这里用到了Express,Express是Node.js世界的web framework。
新建工程
创建工程目录,
为自己的工程建一个
package.json
文件,npm init
会问你一些问题,直接回车表示接受默认值。package.json
的更多信息见此文档。安装依赖
安装Express,
--save
选项会在package.json
文件里加上对express
的依赖。编写代码
如果在执行
npm init
时没有指定新的名字,那么程序的“main”文件就是index.js
。编辑index.js
文件如下,把静态文件放到
public
目录下面。 假设public
目录下面有个index.html
文件,那么就可以通过http://localhost:3000/index.html
来访问文件了。如果想给静态文件指定URL前缀,可以这样(详见文档),
现在可以通过
http://localhost:3000/static/index.html
来访问文件。如果你觉得这篇文章对你有用,可以微信扫一扫表示🙏 / If you find this post is useful to you, buy me 🍶 via Wechat