欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mac下node.js 安装express后报express command not found

程序员文章站 2022-07-12 13:22:36
...

mac下node.js 安装express后报express command not found

使用brew安装node的时候会自动帮你安装npm,所以npm不用单独去安装,当然如果你想安装最新版本,可以去官网下pkg包安装,http://nodejs.org/

 

 

 

 

 

 

Shell

 

brew install node

 

brew install node

这个步骤node和npm就都安装好了

 

 

 

 

 

 

Shell

 

sudo npm install -g express

 

sudo npm install - g express

全局安装express -g 是全局安装,如果想指定版本安装可以用sudo npm install -g [email protected]

注意上面安装的不是命令行工具express(老版本是集成在一起的,现在express4版本,单独分离出来了,放到express-generator包中),而是安装的这个模块,需安装命令行工具需再安装,

 

 

 

 

 

 

Shell

 

sudo npm install -g express-generator

 

sudo npm install - g express - generator

然后就可以使用express命令了,我是官网下载安装的,所以node和npm的路径是 /usr/local/bin 下

 

 

 

 

 

 

Shell

 

54im@IvonMac ~$express -V
4.9.0
54im@IvonMac ~$node -v
v0.10.33
54im@IvonMac ~$npm -v
1.4.28
54im@IvonMac ~$which node
/usr/local/bin/node

 

54im @ IvonMac ~ $express - V

4.9.0

54im @ IvonMac ~ $node - v

v0 . 10.33

54im @ IvonMac ~ $npm - v

1.4.28

54im @ IvonMac ~ $which node

/ usr / local / bin / node

express 是 Node.js 上最流行的 Web 开发框架,正如他的名字一样,使用它我们可以快速的开发一个 Web 应用。我们用 express 来搭建我们的博客,用 express 初始化了一个工程项目,并指定使用 ejs 模板引擎express -e ejs blog

生成.ejs 的模板引擎,项目名为blog的项目

附录

express4.x新特性

  • 健壮的路由系统
  • 包含 HTTP helpers (跳转, 缓存等)
  • 试图系统支持超过 14 中模板引擎
  • Content negotiation
  • 专注高性能
  • 基于 environment 的配置
  • 项目框架生成命令行工具
  • 测试全面

express3.x 迁移到 express4.x

译文原址: http://segmentfault.com/blog/gucheen/1190000000603327

原文: https://github.com/strongloop/express/wiki/Migrating-from-3.x-to-4.x

Express 3.x 到 4.0 的迁移指南。你可能对这篇文章也有兴趣 4.x版中的新功能 。

更多的例子和完整的API文档,请参见 Express 4.x 的文档 。

概述

Express 4 不再依赖 Connect 。这意味着所有捆绑的中间件(除了 static )都不再能从 express 模块中被调用。这些中间件都可以作为下面提及的模块进行调用。这一变化是为了让这些中间件在获取修复,更新和发布的同时不影响 express 的发布周期,反之亦然。

其他模块在这里 https://github.com/senchalabs/connect#middleware

移除

app.configure()

这种方法不再可用。如果你想配置基于环境的不同路由,使用 if 语句或替代模块。

 

 

 

 

 

 

Shell

 

app.configure('development', function() {
   // configure stuff here
});
// 现在改为
var env = process.env.NODE_ENV || 'development';
if ('development' == env) {
   // configure stuff here
}

 

app .configure ( 'development' , function ( ) {

/ / configure stuff here

} ) ;

/ / 现在改为

var env = process .env .NODE_ENV || 'development' ;

if ( 'development' == env ) {

/ / configure stuff here

}

app.router

这个中间件已经全面改版,以此避免 .use 同 .get 之间的混淆(或者是其他HTTP动作),同时,不再需要手动声明的 app.use(app.router) (已被移除)。查看下面关于新的中间件和路由 API 的一节。

如果你的代码看起来像这样:

 

 

 

 

 

 

Shell

 

app.configure('development', function() {
   // configure stuff here
});
// 现在改为
var env = process.env.NODE_ENV || 'development';
if ('development' == env) {
   // configure stuff here
}

 

app .configure ( 'development' , function ( ) {

/ / configure stuff here

} ) ;

/ / 现在改为

var env = process .env .NODE_ENV || 'development' ;

if ( 'development' == env ) {

/ / configure stuff here

}

app.router 已被移除,中间件和路由按照它们添加的顺序被执行。在你的代码中,你应该将原本在 app.use(app.router) 之后的向 app.use 的请求移动到其他路由之后(HTTP动作)。

 

 

 

 

 

 

Shell

 

app.use(cookieParser());
app.use(bodyParser());
/// 其他的中间件,并没有影响
app.use(app.router); // <--- 这行会被移除

// 更多的中间件(在路由之后执行)
app.use(function(req, res, next);
// 处理错误的中间件
app.use(function(err, req, res, next) {});

app.get('/' ...);
app.post(...);

 

app .use ( cookieParser ( ) ) ;

app .use ( bodyParser ( ) ) ;

/ / / 其他的中间件,并没有影响

app .use ( app .router ) ; / / < -- - 这行会被移除

/ / 更多的中间件(在路由之后执行)

app .use ( function ( req , res , next ) ;

/ / 处理错误的中间件

app .use ( function ( err , req , res , next ) { } ) ;

app .get ( '/' . . . ) ;

app .post ( . . . ) ;

express.createServer()

长期弃用。现在只要使用`express()’创建一个新的应用。

connect 中间件

除了 express.static 为了便捷性直接封装在 express 中,其他所有 connect 的中间件都被分离到了独立的模块中。由此,每个独立的模块都可以拥有自己的版本控制。

connect 的补丁

Connect 对 node 的原型进行了全局的改动。

这被认为是不正确的,所以在 Connect 3 中已被移除。

其中一些补丁是:

  • res.on('header')
  • res.charset
  • res.headerSent – use node’s res.headersSent instead

你不应该在任何 Connect 或 Express 的库中再使用这些。

res.charset

如果你想快速设置默认的字符集(你确实应该这么做),

使用 res.set('content-type') 或者 res.type() 来设置 header 。

当使用 res.setHeader() 时,默认的字符集将不会添加。

改动

app.use

app.use 现在可以接受 :params .

 

 

 

 

 

 

Shell

 

app.use('/users/:user_id', function(req, res, next) {
  // req.params.user_id 可以正确获取
});

 

app .use ( '/users/:user_id' , function ( req , res , next ) {

/ / req .params .user_id 可以正确获取

} ) ;

req.accepted()

使用 req.accepts() 来代替。

  • req.accepts()
  • req.acceptsEncodings()
  • req.acceptsCharsets()
  • req.acceptsLanguages()

都在内部使用 accepts 。

请参考 accepts 来提交问题或者查看文档。

请注意,这些属性可能已经从数组改为函数。

要把它们作为“数组”使用,只要不向它们传递任何参数即可。

例如, req.acceptsLanguages() // => ['en', 'es', 'fr'] .

res.location()

不再做相对路径的 URL 解析。浏览器自身将处理相对路径的 URL 。

app.route -> app.mountpath

当把一个 express 应用安装到另一个 express 应用中时。

配置改动

  • json spaces 在开发模式下不再默认启用。

req.params

现在它是一个对象而不是数组。如果你以前使用 req.params[##] 的形式,这不会破坏你的应用,它可以在不知道参数名的情况下,使用正则来处理路由。

res.locals

不再是一个函数。现在这是一个普通的JS对象。所以把它当作普通的对象来处理。

res.headerSent

改为 headersSent 来匹配 node.js 的 ServerResponse 对象。你的应用可能并没有使用到这个,因此它不会是一个问题。

req.is

现在在内部使用 type-is 。

请参考 type-is 来提交问题或者查看文档。

新加入

app.route(path)

返回一个新的 Route 的实例。当收到和这个路由的 path 相匹配的请求时,这个路由就会被调用。路由可以有自己的中间件和针对 HTTP 动作的方法来处理请求。

请参阅路由和路由线路文档,以便更好地了解如何在 express 中创建路由。

Router 和 Route 中间件

Router 已经全面改版,现在它是一个功能完善的中间件路由。Router 是在不牺牲参数匹配和中间件的情况下,将你的路由分离到多个文件或者模块的好方法。请参阅 Routes 及 路由文档 。