首页 > 解决方案 > 如何跟踪错误“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');
});

标签: node.js

解决方案


你拼错了。

const db = mysql.createConnection({
    host:     "localholst",
    user:     "root",
    password: "password",
    database: "nairobi"
});

更改localholstlocalhost


推荐阅读