nodejs连接找不到静态 [英] nodejs connect cannot find static

查看:48
本文介绍了nodejs连接找不到静态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我尝试了其他解决方案 here 但没用

NOTE: I have tried other solution given here but it didn't work

NodeJs 的新手.我正在尝试关注 AngularJS pro,但在设置 NodeJs 服务器时遇到了麻烦.根据书,我安装了nodejs,然后使用安装了连接包npm 安装连接

A newbie with NodeJs. I am trying to follow AngularJS pro and got stuck with setting up NodeJs server. According to book, I installed nodejs and then installed connect package using npm install connect

然后将 angularjs 下载到 nodejs 文件夹旁边的文件夹中.然后编写 server.js 文件以连接到服务器.这是文件的内容:

then downloaded angularjs in folder next to nodejs folder. Then wrote server.js file to connect to server. Here is the content of the file:

    var connect = require('connect');
connect.createServer(connect.static("../angularjs")).listen( 5000);

当我使用以下命令运行这个 server.js 文件时:

When I run this server.js file using:

node server.js

我收到以下错误:

 function app(req, res, next){ app.handle(req, res, next); }
 merge(app, proto);
 merge(app, EventEmitter.prototype);
 app.route = '/';
 app.stack = [];
 return app;
 has no method 'static'
   at Object.<anonymous> (C:web
odejsserver.js:2:36)
   at Module._compile (module.js:456:26)
   at Object.Module._extensions..js (module.js:474:10)
   at Module.load (module.js:356:32)
   at Function.Module._load (module.js:312:12)
   at Function.Module.runMain (module.js:497:10)
   at startup (node.js:119:16)
   at node.js:906:3

大家有什么想法吗?谢谢.

Any ideas guys? Thanks.

推荐答案

connect 包在其代码库的最新 3.x 版本中进行了一些更改,将 static 中间件移至其自己的包裹.您可以在此处查看已移动的软件包列表.

The connect package has made some changes in the latest 3.x version of their code base, moving the static middleware to it's own package. You can view the list of packages that have been moved here.

所以你有两个选择:

选项 1
您可以安装旧的 2.x 版本的 connect 并按原样使用它:

Option 1
You can install an older, 2.x version of connect and use that as is:

$ npm install connect@2.X.X

$ npm install connect@2.X.X

安装最新的 2.X.X 版本将使您当前的实现正常运行.

Installing the latest 2.X.X version will allow your current implementation to function properly.

选项 2
可以继续使用3.x版本的connect,也可以添加serve-static:

$ npm install serve-static

$ npm install serve-static

您还必须更新您的 server.js 文件以包含新的 serve-static 模块:

You would also have to update your server.js file to include the new serve-static module:

var connect = require('connect'),
    serveStatic = require('serve-static');

var app = connect();

app.use(serveStatic("../angularjs"));
app.listen(5000);

这篇关于nodejs连接找不到静态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆