【发布时间】:2018-12-07 04:19:48
【问题描述】:
所以我第一次学习如何用 Mysql 做 Node.js。我目前正在关注本教程(https://hackernoon.com/setting-up-node-js-with-a-database-part-1-3f2461bdd77f)并且我被困在我运行节点的点(在标题“设置 Knex”之前)以及当用户在输入中输入他们想要的用户名和密码时。在教程中它说它应该 console.log 回用户的用户名和密码,但我得到了 undefined。
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()
}
}
【问题讨论】:
标签: html mysql node.js express