Installing a Web Server for Node.js

≯℡__Kan透↙ 提交于 2019-12-07 15:35:34

The reason why connect.static() does not work is that the latest major version of connect (3.x) no longer contains all of the middleware that connect was bundled with in 2.x.

The readme for connect has a list of middleware references that show you the name of the module on npm that gives you the old functionality for each middleware (e.g. static is now broken out into its own module serve-static).

Hilary

I'm following the same book/example and the following works. I claim no credit, it is from another Stack Overflow answer about setting up a simple server plus the contents of a comment on the same answer (question 24346161 link to it from here: nodejs connect cannot find static)

Because I used it in exactly the same learning context (book I also purchased called "Pro Angular JS") and I have been around the houses for 3 hours trying to sort this out (yes a complete novice), I thought I would post it here.

firstly from your node installation directory npm install serve-static

secondly, your node server.js code for a simple static server to serve your angularjs directory contents in a localhost:5000 browser window, on a Windows 7 machine should be (as at July 2015) ...

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

var app = connect();

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

I just stuck a simple index.html file in the angularjs directory to begin with containing

connection working

to test it and it worked a treat.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!