nodejs connect cannot find static

妖精的绣舞 提交于 2019-11-26 14:12:20

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 connect@2.X.X

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

The answer by dylants is helpful. However, here are the exact steps that I followed in order to resolve the same error. 1. From the command window , change directory to the directory in which you installed nodeJS. 2. After having already ran npm install connect, run:

npm install serve-static

3. Create a file named server.js with the following code:

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

var app = connect();

app.use(serveStatic("./angularjs"));
app.listen(5000);
  1. While still in command window, and still in the directory in which you have installed nodeJS, run:

    node server.js

  2. Navigate to the URL http://localhost:5000/test.html

This should work. Here is my directory configuration: C:\NodeJSInstallLocation\angularjs

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

connect().use(
    serveStatic("angularjs")
).listen(5000);

You might want to try something like this

var express = require('express');
var app = express();
app.use(express.static('angularjs'));
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!