参考node环境搭建
在终端输入
$ node -v
v11.12.0
然后用自己习惯的编辑器创建一个server.js,输入如下:
var http = require('http')
var server = http.createServer((req, res) => {
res.writeHead(200, {'Content-type': 'text/html'})
res.end('<marquee behavior="SCROLL" direction="LEFT" bgcolor="#dddddd" height="30" width="150" hspace="0" vspace="0" loop="INFINITE" scrollamount="30" scrolldelay="500">Hello</marquee>')
})
server.listen(3000)
使用如下命令启动服务
node server.js
在浏览器中输入http://localhost:3000 会看到一个marquee标签的动画
NPM
新建一个文件夹
$ mkdir my-project
$ cd my-project/
$ npm init //初始化node工程,生成一个package.json文件,项目配置文件
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>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (my-project) colorword
version: (1.0.0)
description: 打印彩色字体
entry point: (index.js) app.js
test command:
git repository:
keywords:
author: pang
license: (ISC)
About to write to /Users/zhangsan/Documents/nodeDir/my-project/package.json:
{
"name": "colorword",
"version": "1.0.0",
"description": "打印彩色字体",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "pang",
"license": "ISC"
}
Is this OK? (yes)
$ npm install colors
安装完毕my-project目录结构如下:
新建一个app.js文件:
$ vi app.js
require('colors')
console.log('hello wolor'.rainbow)
$ node app.js
运行结果如下:
上传NPM模块
将自己的模块发布出去,首先在https://www.npmjs.com注册一个账户,执行如下命令
$ npm adduser
验证账户密码,如果遇到npm ERR! code E409,可能是registry选项替换成了淘宝的镜像,解决办法:
$ npm login --registry http://registry.npmjs.org
然后执行:
$ npm publish