Ubuntu install nvm and install node steps
First install nvm, type in terminal:
$ wget -qO- https://raw.github.com/creationix/nvm/v0.25.0/install.sh | sh
If the nvm version is a lower version, you can checkout the new version installation in ~/.nvm/, and it will be automatically updated
After installing node: install node directly using nvm
First use nvm to view all existing node versions:
$ nvm ls-remote
Then select the version you want to install:
$ nvm install <version>
After installing multiple versions, you can switch between different versions:
nvm use <version>
You can use nvm current to view the current node version, and node ls to view the local node
Also set the default version with the command:
$ nvm alias default<version>
After installation, you can check the version you have installed with node -v
Here is my output:
wang@wang-B5400:~$ node -v (-v output feedback)
v4.2.2
wang @ wang-B5400: ~ $ npm -v
2.14.7
n and nvm are two commonly used Node.js version management tools
n is an npm package that needs to be installed globally
Module creation, uninstallation, update, search
①Create a module: The package.json file is essential. Use npm to generate the package.json file, the generated file contains the basic resulting command: $ npm init
The npm command to install the node.js module syntax is as follows:
$ npm install <Module Name>
For example, use the npm command to install the common Node.js web framework module express:
$ npm install express
安装好之后,express包就放在了工程目录下的node_modules目录中,因此在代码中只需要通过 require('express') 的方式就好,无需指定第三方包路径:var express = require('express')
②卸载模块:可以使用以下命令来卸载 Node.js 模块
$ npm uninstall express
卸载后,可以到 /node_modules/ 目录下查看包是否还存在,或者使用命令查看:$ npm ls
③更新模块:可以使用命令更新模块:$ npm update express
④搜索模块:可以使用命令来搜索模块:$ npm search express
1.通过 app.set 设置模板引擎为 ejs 和存放模板的目录(将页面模板和数据结合起来生成 html)
2.通过 app.use 挂载到不同的路径
3.通过 app.use 加载中间件(在中间件中通过 next 将请求传递到下一个中间件,next 可接受一个参数接收错误信息,如果使用了 next(error),则会返回错误而不会传递到下一个中间件)
开发环境:Node.js MongoDB Express
(目录结构)对应文件及文件夹的用处:
models: 存放操作数据库的文件
public: 存放静态文件,如样式、图片等
routes: 存放路由文件
views: 存放模板文件
index.js: 程序主文件
package.json: 存储项目名、描述、作者、依赖等等信息
(安装依赖模块):
对应模块的用处:
express: web 框架
express-session: session 中间件
connect-mongo: 将 session 存储于 mongodb,结合 express-session 使用
connect-flash: 页面通知提示的中间件,基于 session 实现
ejs: 模板
express-formidable: 接收表单及文件的上传中间件
config-lite: 读取配置文件
marked: markdown 解析
moment: 时间格式化
mongolass: mongodb 驱动
objectid-to-timestamp: 根据 ObjectId 生成时间戳
sha1: sha1 加密,用于密码加密
winston: 日志
express-winston: 基于 winston 的用于 express 的日志中间件
package.json 位于模块的目录下,用于定义包的属性
package.json 属性说明
name - 包名
version - 包的版本号
description - 包的描述
homepage - 包的官网 url
author - 包的作者姓名
contributors - 包的其他贡献者姓名
dependencies - 依赖包列表。如果依赖包没有安装,npm 会自动将依赖包安装在 node_module 目录下
repository - 包代码存放的地方的类型,可以是 git 或 svn,git 可在 Github 上
main - main 字段是一个模块ID,它是一个指向你程序的主要项目。就是说,如果你包的名字叫 express,然后用户安装它,然后require("express")
keywords - 关键字