nodejs连接找不到静态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24346161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:31:17  来源:igfitidea点击:

nodejs connect cannot find static

node.jsconnect

提问by Sheraz

NOTE: I have tried other solution given herebut 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

NodeJs 的新手。我正在尝试关注 AngularJS pro,但在设置 NodeJs 服务器时遇到了麻烦。根据书,我安装了 nodejs,然后使用 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:

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

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

When I run this server.js file using:

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

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\nodejs\server.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.

有什么想法吗?谢谢。

回答by dylants

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

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

So you have two options:

所以你有两个选择:

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

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

$ npm install [email protected]

$ npm install [email protected]

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

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

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

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

$ npm install serve-static

$ npm install serve-static

You would also have to update your server.jsfile to include the new serve-staticmodule:

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

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

var app = connect();

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

回答by jbooker

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:

dyants 的回答很有帮助。但是,以下是我为解决相同错误而遵循的确切步骤。1. 在命令窗口中,将目录更改为您安装 nodeJS 的目录。2. 运行 npm install connect 后,运行:

npm install serve-static

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

3. 使用以下代码创建一个名为 server.js 的文件:

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

  1. 仍然在命令窗口中,并且仍在安装 nodeJS 的目录中,运行:

    节点服务器.js

  2. 导航到 URL http://localhost:5000/test.html

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

这应该有效。这是我的目录配置:C:\NodeJSInstallLocation\angularjs

回答by Gautam Anand

You might want to try something like this

你可能想尝试这样的事情

var express = require('express');
var app = express();
app.use(express.static('angularjs'));

回答by Aleksander Jedynak

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

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

回答by Nexson Oandasan

Try this...

尝试这个...

  const express = require('express');
  const app = express();
  const path = require('path');

  app.use('',express.static(path.join(__dirname, '/static/')));