$ d: $ cd git $ git clone https://github.com/cnpm/nvmw.git
set "PATH=d:\git\nvmw;%PATH%"
$ nvmw Usage: nvmw help Show this message nvmw install [version] Download and install a [version] nvmw uninstall [version] Uninstall a [version] nvmw use [version] Modify PATH to use [version] nvmw ls List installed versions Example: nvmw install v0.6.0 Install a specific version number nvmw use v0.6.0 Use the specific version
$ nvmw install 0.12.0
例如:下载koa $ npm install koa --registry=http://registry.npm.taobao.org
$ npm install cnpm -g --registry=http://registry.npm.taobao.org
express server
cd server node bin/www
http://localhost:3000/
var express = require('express'); // 调用 express 实例,它是一个函数,不带参数调用时,会返回一个 express 实例,将这个变量赋予 app 变量。 var app = express(); // app 自己有不少方法,其中包括最经常使用的 get、post、put/patch、delete,在这里咱们调用其中的 get 方法,为咱们的 `/` 路径指定一个 handler 函数。 // 这个 handler 函数会接收 req 和 res 两个对象,他们分别是请求的 request 和 response。 // request 中包含了浏览器传来的各类信息,好比 query 啊,body 啊,headers 啊之类的,均可以经过 req 对象访问到。 // res 对象,咱们通常不从里面取信息,而是经过它来定制咱们向浏览器输出的信息,好比 header 信息,好比想要向浏览器输出的内容。这里咱们调用了它的 #send 方法,向浏览器输出一个字符串。 app.get('/', function (req, res) { res.send('Hello World'); }); // 定义好咱们 app 的行为以后,让它监听本地的 3000 端口。这里的第二个函数是个回调函数,会在 listen 动做成功后执行,咱们这里执行了一个命令行输出操做,告诉咱们监听动做已完成。 app.listen(3000, function () { console.log('app is listening at port 3000'); });
// 引入依赖 var express = require('express'); var utility = require('utility'); // 创建 express 实例 var app = express(); app.get('/', function (req, res) { // 从 req.query 中取出咱们的 q 参数。 // 若是是 post 传来的 body 数据,则是在 req.body 里面,不过 express 默认不处理 body 中的信息,须要引入 https://github.com/expressjs/body-parser 这个中间件才会处理,这个后面会讲到。 // 若是分不清什么是 query,什么是 body 的话,那就须要补一下 http 的知识了 var q = req.query.q; // 调用 utility.md5 方法,获得 md5 以后的值 // 之因此使用 utility 这个库来生成 md5 值,其实只是习惯问题。每一个人都有本身习惯的技术堆栈, // 我刚入职阿里的时候跟着苏千和朴灵混,因此也混到了很多他们的技术堆栈,仅此而已。 // utility 的 github 地址:https://github.com/node-modules/utility // 里面定义了不少经常使用且比较杂的辅助方法,能够去看看 var md5Value = utility.md5(q); res.send(md5Value); }); app.listen(3000, function (req, res) { console.log('app is running at port 3000'); });
const express = require('express'); const superagent = require('superagent'); const cheerio = require('cheerio'); const app = express(); app.get('/', function (req, res, next) { // 用 superagent 去抓取 https://cnodejs.org/ 的内容 superagent.get('https://cnodejs.org/') .end(function (err, sres) { // 常规的错误处理 if (err) { return next(err); } // sres.text 里面存储着网页的 html 内容,将它传给 cheerio.load 以后 // 就能够获得一个实现了 jquery 接口的变量,咱们习惯性地将它命名为 `$` // 剩下就都是 jquery 的内容了 var $ = cheerio.load(sres.text); var items = []; $('#topic_list .topic_title').each(function (idx, element) { var $element = $(element); items.push({ title: $element.attr('title'), href: $element.attr('href') }); }); res.send(items); }); }); app.listen(3000, function () { console.log('app is listening at port 3000'); });
const express = require('express'); const logger = require('morgan'); const app = express(); app.use(logger('short')); app.use((req,res)=>{ res.writeHead(200, {"Content-Type": "text/plain"}); res.end('vb, vb') }) app.listen(3000,()=>{ console.log('兼听成功'); })
const express = require('express'); const path = require('path'); const app = express(); const pulbicPath = path.resolve(__dirname, "public"); app.use(express.static(pulbicPath)); app.use((req,res) =>{ res.writeHead(200, { "Content-Type": "text/plain" }); res.end("Looks like you didn't find a static file."); }) app.listen(3000,()=>{ console.log("兼听成功"); })
const express = require('express'); const logger = require('morgan'); const path = require('path'); const app = express(); //获取输出日志信息 app.use(logger('short')); // 全部的请求经过这个中间件,若是没有文件被找到的话会继续前进 const publicPath = path.resolve(__dirname, "public"); app.use(express.static(publicPath)); // 当请求根目录的时候被调用 app.get("/", (req, res) => { res.end("home"); }); app.get("/about", (req, res) => { res.end("about"); }); app.get("/other", (req, res) => { res.end("ohter") }) //除了固定路由形式外,它还能够匹配更复杂的路由(使用正则等方式): app.get("/home/:other", (req, res) => { // :other 并非固定住,它表示 URL 中传递过来的名字 res.end("hello"+req.params.other) }) // 前面都不匹配,则路由错误。返回 404 页面 app.use((req, res) => { res.statusCode = 404; res.end("404"); }) app.listen(3000, () => { console.log("suceess!"); });
设置重定向 app.get("/my", (req, res) => { res.redirect("/home/my"); res.end("my=>redirect"); })
response.sendFile("path/to/cool_song.mp3")
var express = require("express"); var app = express(); var EVIL_IP = "123.45.67.89"; app.use(function(request, response, next) { if (request.ip === EVIL_IP) { response.status(401).send("Not allowed!"); } else { next(); } });
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Hello, world!</title> </head> <body> <%= message %> </body> </html>
const express = require('express') const path = require('path') const app = express(); // 告诉 Express 你的视图存在于一个名为 views 的文件夹中 app.set("views", path.resolve(__dirname, "views")) // 告诉 Express 你将使用EJS模板引擎 app.set("view engine", "ejs") app.get("/", (req, res) => { res.render('index', { message: "hello ejs" }) }) app.listen(3000, () => { console.log("susscess"); })
{ "name": "le", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "start": "node app" }, "author": "", "license": "ISC", "dependencies": { } }
const express = require("express"); const path = require("path"); const logger = require("morgan"); const bodyParser = require("body-parser"); const app = express(); // 告诉 Express 你的视图存在于一个名为 views 的文件夹中 app.set("views", path.resolve(__dirname, "views")) // 告诉 Express 你将使用EJS模板引擎 app.set("view engine", "ejs") //设置留言的全局变量 const entries = []; app.locals.entries = entries; //使用morgan 进行日志记录 app.use(logger("dev")); // 设置用户表单提交动做信息的中间件,全部信息会保存在 req.body 里 app.use(bodyParser.urlencoded({extended: false})); // 当访问了网站根目录,就渲染主页(位于views/index.ejs) app.get("/", (req, res) => { res.render("index"); }) // 渲染“新留言”页面(位于views/index.ejs)当get访问这个URL的时候 app.get("/new-entry", (req, res) => { res.render("new-entry"); }) // POST 动做进行留言新建的路由处理 app.post("/new-entry", (req, res) => { // 若是用户提交的表单没有标题或者内容,则返回一个 400 的错误 if(!req.body.title || !req.body.body){ res.status(400).send("Entries must have a title and a body."); return; } // 添加新留言到 entries 中 entries.push({ title: req.body.title, content: req.body.body, published: new Date() }) // 重定向到主页来查看你的新条目 res.redirect("/"); }) // 渲染404页面,由于你请求了未知资源 app.use((req, res) => { res.status(404).render("404"); }) app.listen(3000, () => { console.log("susscess!"); })
4.新建文件夹view,在view中新建header.ejscss
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Express Demo</title> <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> </head> <body class="container"> <h1> Express Guestbook <a href="/new-entry" class="btn btn-primary pull-right"> Write in the guestbook </a> </h1>
5.在views文件中新建footer.ejshtml
</body> </html>
6.在views文件中新建index.ejsnode
<% include header.ejs %> <% if (entries.length) { %> <% entries.forEach(function(entry) { %> <div class="panel panel-default"> <div class="panel-heading"> <div class="text-muted pull-right"> <%= entry.published %> </div> <%= entry.title %> </div> <div class="panel-body"> <%= entry.body %> </div> </div> <% }) %> <% } else { %> No entries! <a href="/new-entry">Add one!</a> <% } %> <% include footer.ejs %>
7.在views文件中新建new-entry.ejsjquery
<% include header %> <h2>Write a new entry</h2> <form method="post" role="form"> <div class="form-group"> <label for="title">Title</label> <input type="text" class="form-control" id="title" name="title" placeholder="Entry title" required> </div> <div class="form-group"> <label for="content">Entry text</label> <textarea class="form-control" id="body" name="body" placeholder="Love Express! It's a great tool for building websites." rows="3" required></textarea> </div> <div class="form-group"> <input type="submit" value="Post entry" class="btn btn-primary"> </div> </form> <% include footer %>
8.在views文件中新建404.ejsgit
<%include header.ejs%> <h2>404! Page not found.</h2> <%include footer.ejs%>