node_modules中ExpressJS的脚本路径 [英] Script path for ExpressJS in node_modules

查看:103
本文介绍了node_modules中ExpressJS的脚本路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  doctype html 
html
head
title = title
link(rel ='stylesheet',href ='/ stylesheets / main.css')
script(src ='.. / node_modules / some_package / package_script。 min.js')

我的路由文件夹中有一个路由器的index.js:

  router.get('/',function(req,res){
res.render('index',{title :'title goes here'})
});

当我尝试访问控制台中的玉器文件中的node_modules文件夹时,我不断收到404错误:
GET /node_modules/some_package/package_script.min.js 404 1ms - 19b



文件路径是否正常工作/当我尝试访问node_modules中的脚本时,如何修复Express?我需要复制必要的JavaScript文件,并将它们放在公共下的javascripts文件夹下工作吗?



谢谢!

解决方案

哇!保持... node_modules 是一个私人目录,不应该被公开。默认情况下, public 目录是静态文件的公共路径:

  app.use(express.static(path.join(__ dirname,'public'))); 

有一个 javascripts 目录。将您的脚本复制到您的模板中,将 src 属性更改为:

  /javascripts/package_script.min.js 

如果您真的想提供 node_modules ,这将是一个坏主意,在 app.use(express.static(...))之下添加以下内容; 行以上:

  app.use(express.static(path.join(__ dirname,'node_modules'))); 


I have an index.jade in my 'views' folder.

doctype html
html
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/main.css')
    script(src='../node_modules/some_package/package_script.min.js')

I have index.js in my routes folder with a router:

router.get('/', function(req, res) {
  res.render('index', { title: 'title goes here' })
});

I keep getting a 404 error when I try to access the node_modules folder in the jade file in the console: GET /node_modules/some_package/package_script.min.js 404 1ms - 19b

How exactly does the file pathing work / how can I fix it for Express when I'm trying to access a script in the node_modules? Do I need to copy the necessary javascript files and place them under the 'javascripts' folder under 'public' for it to work?

Thanks!

解决方案

Whoa! Hold on... node_modules is a private directory and shouldn't be exposed. By default the public directory is the public root path for static files:

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

In there is a javascripts directory. Copy your script there and change the src attribute in your template to:

/javascripts/package_script.min.js

If you really wanted to serve up node_modules, which would be a bad idea, add the following beneath the app.use(express.static(...)); line above:

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

这篇关于node_modules中ExpressJS的脚本路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆