简体   繁体   中英

Running client-side javascript on a Node.js project

I'm building a user registration / authentication system in Node.js. On the registration form, the user is prompted to type their email, password, and confirm their password. Before sending the email and the password to the server to be encrypted and stored, I want to check whether the password strings match. I don't want to send both passwords to Node.js to have it check whether they match. Instead, I want to compare the password field values on the client-side, and only submit once the passwords match. I'm using EJS as my templating engine.

  • How can I run client-side Javascript on a page loaded from Node.js? This seems like it should be a simple question, but in 3 days' searching, I haven't found a good answer.

  • How can I include jQuery for selectors and event handlers for this code?

与对非Node.js项目的操作完全相同:在EJS中使用<script>标记包括一个javascript文件(或代码)。

To execute the javascript logic you've written in node on the client I recommend running it through http://browserify.org/ .

This will enable you to use the same logic written in node on the client.

Also, you can npm install jquery:

var $ = require('jquery');

For your client side bundle. But yes you will still need to serve the build javascript with a script tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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