简体   繁体   English

使用Node.JS,Express和EJS的browserify实现示例

[英]Example of browserify implementation with Node.JS, Express, and EJS

I'm having trouble getting browserify to work on my web app. 我无法让browserify在我的Web应用程序上正常工作。

I browserify in the terminal and it creates the bundle.js file. 我在终端中使用browserify并创建bundle.js文件。 I put it in my public/javascripts folder and included it in my EJS file, but still can't get code to work: 我将其放在我的public / javascripts文件夹中,并将其包含在我的EJS文件中,但仍然无法使代码正常工作:

terminal: 终奌站:

browserify public/javascripts/chat.js > bundle.js

chat.js file: chat.js文件:

$(function() {
    var parse = require("parse").Parse;
    let query = new Parse.Query('Chat');
    let subscription = query.subscribe();

    subscription.on('open', () => {
        //console.log('subscription opened');
        alert("sub opened");
    });

    subscription.on('create', (object) => {
        //console.log('object created: ' + object.get('message'));
        alert('object created: ' + object.get('message'))
    });
});

ejs file: ejs文件:

<script src="javascripts/bundle.js "></script>

src="javascripts/bundle.js" is a relative path . src="javascripts/bundle.js"相对路径 when your app comes to this path it will look for a folder named javascript within the same folder that your server.js (or whatever files which running node process ) is and cannot find it; 当你的应用程序来这个路径,它将寻找一个文件夹命名javascript您的文件夹内server.js (或任何文件,其运行的节点处理),并不能找到它; Cuz i am pretty sure you are not going to serve your back-end app within your public folder. 因为我很确定您不会在公用文件夹中提供您的后端应用程序。

I think you meant src="/javascripts/bundle.js" . 我认为您的意思是src="/javascripts/bundle.js" Now it is an absolute path and if you're serving static files from public folder: 现在,它是一个绝对路径 ,如果你正在处理的是静态文件public文件夹:

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

this file will serve from http://yourhost/javascripts/bundle.js regardless of where you server.js is positioned or running. 无论server.js的位置或运行位置如何,此文件都可以从http://yourhost/javascripts/bundle.js

UPDATE: Didn't realize this, but I had to include the chat.js script beneath the bundle.js script and got it to work. 更新:没意识到这一点,但是我不得不在bundle.js脚本下面包含chat.js脚本,然后使其生效。 Thanks @Dnitro 谢谢@Dnitro

Changed it to: 更改为:

<script src="javascripts/bundle.js "></script>
<script src="javascripts/chat.js "></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM