I\'m trying to start serving some static web pages using connect
like this:
var connect = require(\"connect\");
var nowjs = require(\"now\");
va
You may be here because you're reading the Apress PRO AngularJS
book...
As is described in a comment to this question by KnarfaLingus
:
[START QUOTE]
The connect module has been reorganized. do:
npm install connect
and also
npm install serve-static
Afterward your server.js
can be written as:
var connect = require('connect');
var serveStatic = require('serve-static');
var app = connect();
app.use(serveStatic('../angularjs'));
app.listen(5000);
[END QUOTE]
Although I do it, as the book suggests, in a more concise way like this:
var connect = require('connect');
var serveStatic = require('serve-static');
connect().use(
serveStatic("../angularjs")
).listen(5000);
The easiest way to serve static files is to use "harp". It can be found here. You can serve up your files from the location you want via node is:
var harp = require("harp")
harp.server(projectPath [,args] [,callback])
Hope this helps.
This code should work:
var connect = require("connect");
var app = connect.createServer().use(connect.static(__dirname + '/public'));
app.listen(8180);
Also in connect 2.0 .createServer() method deprecated. Use connect() instead.
var connect = require("connect");
var app = connect().use(connect.static(__dirname + '/public'));
app.listen(8180);
You'll see the message Cannot GET /
if you don't specify which page it is that you're trying to get, in other words if your URL is something like http://localhost:8180
. Make sure you enter a page name, e.g. http://localhost:8180/index.html
.
You may also want to try st, a node module for serving static files. Setup is trivial.
npm install connect
npm install st
And here's how my server-dev.js file looks like:
var connect = require('connect');
var http = require('http');
var st = require('st');
var app = connect()
.use(st('app/dev'));
http.createServer(app).listen(8000);
or (with cache disabled):
var connect = require('connect');
var http = require('http');
var st = require('st');
var app = connect();
var mount = st({
path: 'app/dev',
cache: false
});
http.createServer(function (req, res) {
if (mount(req, res)) return;
}).listen(8000);
app.use(mount);
var connect = require('connect');
var serveStatic = require('serve-static');
var app = connect();
app.use(serveStatic('../angularjs'), {default: 'angular.min.js'}); app.listen(3000);