Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
125 views
in Technique[技术] by (71.8m points)

node.js - nodejs connect cannot find static

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

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

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);

When I run this server.js file using:

node server.js

I get following error:

 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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

So you have two options:

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

$ npm install [email protected]

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

Option 2
You can continue to use the 3.x version of connect, and also add serve-static:

$ npm install 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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...