所以我第一次学习如何用Mysql做Node.js。我目前正在学习这篇教程(https://hackernoon.com/setting-up-node-js-with-a-database-part-1-3f2461bdd77f),我被困在运行node的地方(在标题'Setting up Knex‘之前),当用户在输入中输入他们想要的用户名和密码时。在教程中,它说应该console.log回用户的用户名和密码,但我得到的却是未知的。
Server running on http://localhost:7555
Add user undefined with password undefined
我试着寻找如何解决它,但我似乎不能有我的工作。我不太确定它是express还是html可能看起来已经过时了。这就是我现在拥有的。
index.html
<!DOCTYPE html>
<html>
<head>
<title>Node Database Tutorial</title>
</head>
<body>
<h1>Create a new user</h1>
<form action="/CreateUser", method="post">
<input type="text" class="username" placeholder="username">
<input type="password" class="password" placeholder="password">
<input type="submit" value="Create user">
</form>
<script src="/app.js"><script>
</body>
</html>
app.js
const CreateUser = document.querySelector('.CreateUser')
CreateUser.addEventListener('submit', (e) => {
e.preventDefault()
const username = CreateUser.querySelector('.username').value
const password = CreateUser.querySelector('.password').value
post('/createUser', { username, password })
})
function post(path, data){
return window.fetch(path, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
}
index.js
const express = require('express')
const bodyParser = require('body-parser')
const store = require('./store')
const app = express()
app.use(express.static('public'))
app.use(bodyParser.urlencoded({extended:false}))
app.use(bodyParser.json())
var jsonParser = bodyParser.json()
var urlencodedParser = bodyParser.urlencoded({extended: false})
app.post('/createUser', urlencodedParser, function(req, res){
if (!req.body) return res.sendStatus(400)
store.createUser({
username: req.body.username,
password: req.body.password
})
.then(() => res.sendStatus(200))
})
app.listen(7555, () => {
console.log('Server running on http://localhost:7555')
})
请帮帮忙,我已经被困了好几天了。
编辑:这是我的console.log所在的位置(store.js)
module.exports = {
createUser({ usern-ame, password }) {
console.log(`Add user ${username} with password ${password}`)
return Promise.resolve()
}
}
https://stackoverflow.com/questions/53658767
复制相似问题