简体   繁体   English

使用 MySQL 和 Javascript (node.js)

[英]Using MySQL with Javascript (node.js)

I've just started learning Node.js.我刚刚开始学习 Node.js。 I come from a PHP background, so I started with MySQL.我来自 PHP 背景,所以我开始使用 MySQL。 So, I wanted to try executing some basic MySQL queries using javaScript.I write the queries, this way:所以,我想尝试使用 javaScript 执行一些基本的 MySQL 查询。我这样写查询:

filename: test.js

var mysql = require('mysql');
var con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database: "test",
});
con.connect(err => {
  if (err) throw err;
  console.log("Connected!");
  const q = "INSERT INTO testing VALUES(3, 'MILLENNIAL', 19, 'STUDENT', 'CSE')";
  con.query(q, (err, result) => {
    if (err) throw err;
    console.log("done");
  });
});

When I run the only the javascript file this way: node test.js the query gets executed but when I connect the same file to a html file, this way:当我以这种方式运行唯一的 javascript 文件时: node test.js查询被执行但是当我将同一个文件连接到一个 html 文件时,这样:

<script src="test.js"></script>

the query doesn't get executed... It gives an error in console: Uncaught ReferenceError: require is not defined查询没有被执行......它在控制台中出现错误: Uncaught ReferenceError: require is not defined错误Uncaught ReferenceError: require is not defined

I have feeling that I am not using something very important here.我觉得我没有在这里使用非常重要的东西。 Am I missing something??我错过了什么吗?? Please help me go further.请帮助我走得更远。

I am so sorry if this is very silly but, please help me with this.如果这很愚蠢,我很抱歉,但是请帮助我。 Thank you in advance.先感谢您。

node.js is javascript runtime which uses javascript syntax, which is meant for server-side language. node.js 是 javascript 运行时,它使用 javascript 语法,用于服务器端语言。 What you trying is to use nodejs script in frontend.您尝试的是在前端使用 nodejs 脚本。 require is not defined - because its applicable only for node, to use other modules in plain javascript you will need to use cdn or packages like browserify, webpack. require 未定义 - 因为它仅适用于节点,要在纯 javascript 中使用其他模块,您将需要使用 cdn 或诸如 browserify、webpack 之类的包。 Checkout docs if you are interested.如果您有兴趣,请查看文档。

I will try to explain from a PHP perspective.我将尝试从 PHP 的角度进行解释。

When you write PHP, usually your resulting file is an HTML and you embed your PHP code in it, so it renders out without PHP to the requesting browser.当您编写 PHP 时,通常您生成的文件是 HTML 并且您将 PHP 代码嵌入其中,因此它在没有 PHP 的情况下呈现给请求浏览器。

In node, this is different.在节点中,这是不同的。 We don't write the HTML with node code embedded.我们不会编写嵌入节点代码的 HTML。 Node focuses more on backend and frontend separation. Node 更注重后端和前端的分离。 You write your backend to reply to your front end.你写你的后端来回复你的前端。

The backend is where you type var mysql = require('mysql');后端是您键入var mysql = require('mysql');

You can use something like express vs koa to serve the resulting query to the front end.您可以使用诸如 express 与 koa 之类的东西将结果查询提供给前端。

Here's a good tutorial to start https://geshan.com.np/blog/2020/11/nodejs-mysql-tutorial/ I usually recommend Koa because it's up updated more regularly now, but express is basically the same thing!这是一个很好的入门教程https://geshan.com.np/blog/2020/11/nodejs-mysql-tutorial/我通常推荐 Koa 因为它现在更新更频繁,但 express 基本上是一样的!

Hope this directs you in the right direction.希望这能引导您朝着正确的方向前进。

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

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