node.js - 如何跟踪错误“getaddrinfo ENOTFOUND localhost 3306”
问题描述
const express = require('express');
const mysql = require('mysql');
const session = require('express-session');
const path = require('path');
const bodyParser = require('body-parser');
const db = mysql.createConnection({
host: "localholst",
user: "root",
password: "password",
database: "nairobi"
});
db.connect((err) =>{
if (err) throw err;
console.log('database connected');
})
const app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(express.static('public'));
app.get('/', (req, res) =>{
res.sendFile(path.join(__dirname + '/index.html'));
});
app.post('/auth', (req, res) =>{
const username = req.body.username;
})
app.listen(3000, () =>{
console.log('Server started on port 3000');
});
解决方案
你拼错了。
const db = mysql.createConnection({
host: "localholst",
user: "root",
password: "password",
database: "nairobi"
});
更改localholst
为localhost
。
推荐阅读
- java - MVC:模型或控制器/视图中的坐标和形状方法?
- css - 如何在带有ruby on rails的伪元素中显示来自svg sprite的图标
- laravel - 发生错误:ArtisanLambdaFunction - 解压后的大小必须小于 220606645 字节(服务:Lambda,状态码:4000
- mongodb - 如何使用 mongodb 在环回 4 中实现 groupBy?
- react-native - 从图库中获取图像的 url 并将图像从设备转换为 tensor3d
- git - 如果不存在标签,则获取第一次提交
- java - Android Studio (Windows) 中的 Flutter 错误:无法识别的选项:--add-opens
- reactjs - 如何使用 Storybook 制作别名?
- django - 通用 DeleteView 在 0x04725628 返回 django.db.models.query_utils.DeferredAttribute 对象 - Django
- microsoft-graph-api - ms botframework 附件无法生成共享链接