节点js如何使用“node_modules”库而不是“src = <cdn>”
我知道这是一个反复出现的问题,但我没有看到我完全理解的答案。
我正在使用我的节点js应用程序,在我的index.html一些cdn地址。
即:
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.3.1/angular-ui-router.min.js"></script>
但我也有这个文件夹在我的node_modules
目录..所以我真的不需要使用CDN。 我只能使用我的node_modules目录中的文件。
问题是如何? 现在我有这个:
app.use(express.static(path.join(__dirname, '/')));
在我的server.js文件中。
但这不是我的节点mudles文件夹(这是在我的index.html相同的根path下)的path。
我试图写一些像:
app.use('/scripts', express.static(__dirname + '/node_modules/'));
并在我的index.html中写下如下内容:
<script src="scripts/angular-ui-router/angular-ui-router.min.js"></script>
但是当我到localhost时,应用程序甚至没有启动:3000因为我得到这个错误:
Error: ENOENT, stat ...... at Error (native)
我的问题是:1.我需要写在我的server.js而不是app.use(express.static(path.join(__dirname, '/')));
2.我需要写在我的index.html文件中,而不是:
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.3.1/angular-ui-router.min.js"></script>
这是我刚刚添加时收到的消息:
app.use('/scripts', express.static(path.join(__dirname, 'node_modules')));
错误:
Error: ENOENT, stat 'C:\<path_to_my_project>\server_modules\index.html' at Error (native)
我不知道为什么它在那里寻找文件…我有一个名为“server_modules”的文件夹,但这不是我的index.html所在的位置…这是我的文件结构
- my_project - - server.js - - index.html - - node_modules - - server_modules
这是我的server.js
// ---------------- define third Party dependencies) var express = require('express'); var app = express(); var http = require('http').Server(app); var path = require('path'); var assert = require('assert'); var mongoose = require('mongoose'); var bodyParser = require('body-parser'); var jwt = require('jsonwebtoken'); var expressJwt = require('express-jwt'); // ---------------- config the app app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); app.use('/scripts', express.static(path.join(__dirname, 'node_modules'))); // ---------------- define the server handlers (internal dependencies) var dbHandler = require('./server_modules/dbHandler.js'); var httpRequesetHandler = require('./server_modules/httpRequesetHandler.js').getHttpRequestHandler(dbHandler, app,jwt,expressJwt); var io = require('./server_modules/socketHandler.js').listen(http, dbHandler); // ---------------- start the server http.listen((process.env.PORT || 3000), function () { dbHandler.get_random_room(); console.log('listening on *:3000 ' + __dirname); });
另外:我在我的单页应用程序使用angular度路由
谢谢
不知道为什么你会得到你得到的错误。 我能够做你成功的问题。 我的步骤:
-
npm install --save angular-ui-router
- 在我的index.hml中,我写道:
<script src='/scripts/angular-ui-router/release/angular-ui-router.min.js' type='text/javascript'></script>
- 在server.js中,我添加了:
app.use('/scripts', express.static(path.join(__dirname, 'node_modules')));
冉它,没问题。
看看你的代码,它看起来像是指向一个不应该存在的基于angular-ui模块结构的文件path。 您还应该在html文件中观察path语法,“scripts / whatever”与“/ scripts / whatever”不一样。
**编辑**
如果您想要从多个位置提供静态文件,则只需多次声明中间件,例如:
// serve files in /public directory from the root of the site, eg http://example.com/index.html would serve the file /public/index.html app.use(express.static(path.join(__dirname, 'public'))); // also serve files in /node_modules from /scripts. So /node_modules/angular-ui-router/release/angular-ui-router.js would be found at /scripts/angular-ui-router/release/angular-ui-router.js app.use('/scripts', express.static(path.join(__dirname,'node_modules')));
- 如何searchJSON对象以查找特定的参数值?
- 父级ng-click事件后,angular度子级ng-click事件不会触发
- 如何使用Angular和Node Express将CSRF与Redis Session配合使用,会话过期后如何重置会话信息和CSRF?
- 使用scrapy python抓取NodeJs和AngularJs网站
- 如何将这些angularjs,visual studio和nodejs结合起来
- 仪表板中的多个模块列表 – Yeoman MEANJS Generator
- 在Express中AngularJS作为前端时未定义会话
- 无法加载资源:服务器在angular.js中的状态为404(未find)
- MongoDB _id长度为25