leuisken.github.io/posts_2014-2016/2014-12-31-testUnhappy.html
2016-04-19 16:44:27 +08:00

62 lines
2.7 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: 现在好多node书在讲express的时候命令都不能正常执行不开心
labels: ["Node", "Express"]
description: 在看node的时候比如《Node.js 开发指南》啦《Node.js实战》啦都是要讲express的。但是怎么说呢书上一开始的命令至少是在Windows平台上总是会出问题。
---
<p>在看node的时候比如《Node.js 开发指南》啦《Node.js实战》啦都是要讲express的。但是怎么说呢书上一开始的命令至少是在Windows平台上总是会出问题。比如现在我手上这本《Node.js实战》吧给出了如下命令</p>
{% highlight console %}
npm install -g express
express -e blog
cd blog &amp; npm install
{% endhighlight %}
<p>但对于现在的版本(4.0)还要加上一条才能在cmd里使用express。</p>
{% highlight console %}
npm install -g express-generator
{% endhighlight %}
<p>很多人都会在这里推荐去看看express的官方文档我在这里发个传送门。<a href="http://www.expressjs.com.cn/">Express中文站</a></p>
<p>刚刚去百度了一下,发现了一篇文章,发个<a href="http://www.cnblogs.com/dacheng/p/nodejs.html">链接</a>过来。</p>
<p>这里反应的也是这个express版本的更新问题啦里面说了要用老版本要用加上@3标明使用版本受教了</p>
{% highlight console %}
npm install -g express-generator@3
{% endhighlight %}
<p>以及书上用node app.js来启动项目现在已经是node start了。你可以打开app.js看看确实是没有了监听3000端口的代码。</p>
<p>打开package.json发现了这么一句。</p>
{% highlight json %}
"start": "node ./bin/www"
{% endhighlight %}
<p>再打开./bin/www看到里面有如下代码。</p>
{% highlight javascript %}
#!/usr/bin/env node
var debug = require('debug')('blog');
var app = require('../app');
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
{% endhighlight %}
<p>可以知道,打开端口监听的代码在这里。</p>
<p>算啦,把原来的包都删掉好了,因为书上讲的跟实例有的代码好大差别的说。。。。。。</p>
<p>这篇博文里面有一些<a href="http://my.oschina.net/robinjiang/blog/168732">常用的npm命令</a>,可以移步参考下。</p>
<p>于是直接执行下面的命令,把之前犯得错误清理掉。</p>
{% highlight console %}
npm ls -g
npm remove express -g
npm remove express-generator -g
npm install -g express@3
{% endhighlight %}
<p>ok了这个世界又清净了。</p>
<p>突然发现这些外部链接会把你们转到别的页面上去啊,写个&lt;base&gt;标签好了。你不管嘛。</p>