【发布时间】:2019-09-10 20:55:41
【问题描述】:
在某一时刻,端口处于活动状态且正在工作,但 Postman 中没有响应。
现在它甚至无法识别 express,即使它被包含在内。谁能帮帮我,我这几天一直在努力解决这个问题...
我尝试更改为使用 knex 连接到 postgres 数据库,在 Pg admin 中创建了数据库,但它不起作用。我想有几条路线来登录、注册和删除用户配置文件,但似乎 Express 出于某种原因无法正常工作。我试图将 app.post(register) 中的 .catch 更改为 .catch (err => res.json(err)),我还为 Chrome 安装了 corse 扩展,但它也不起作用。我修改了代码中的一些单词(使用我的母语,以便更容易查看,如果有遗漏的地方,我深表歉意......我不得不粘贴整个代码,因为错误可能在我省略的部分中。 . 此时,在输出中,Express 没有被识别为已安装。
const express = require('express');
const bodyParser = require('body-parser');
const bcrypt = require('bcrypt-nodejs');
const cors = require('cors');
const knex = require('knex');
const app = express();
app.use(bodyParser.json());
app.use(cors());
let db = knex({
client: 'pg',
connection: {
host : '127.0.0.1',
user : 'postgres',
password : '12345',
database : 'users'
}
});
/*app.get('/', (req, res) => {
db.select('*').from('users').then(data => {res.json(data)});
})*/
//USER LOGIN
app.post('/signin', (req, res) => {
db.select('email', 'hash').from('login')
.where('email', '=', req.body.email)
.then(data => {
const isValid = bcrypt.compareSync(req.body.password, data[0].hash);
if (isValid) {
return db.select('*').from('login')
.where('email', '=', req.body.email)
.then(user => {
res.json(user[0])
})
.catch(err => res.status(400).json('Cannot find user'))
} else {
res.status(400).json('Incorrect log in data')
}
})
.catch(err => res.status(400).json('Incorrect data'))
});
// REGISTER USER
app.post('/register', (req, res) => {
const {email, name, password} = req.body;
const hash = bcrypt.hashSync(password);
db.transaction(trx => {
trx.insert({
hash: hash,
email: email
})
.into('login')
.returning('email')
.then(loginEmail => {
return trx('users')
.returning('*')
.insert({
email: loginEmail[0],
name: name,
resgistered: new Date()
})
.then(user => {
res.json(user[0]);
})
})
.then(trx.commit)
.catch(trx.rollback)
})
.catch(err =>res.json(err))
});
// user profile
app.get('/profile/:id', (req, res) => {
const {id} = req.params;
db.select('*').from('users').where( {id: id})
.then(user => {
if (user.length) {
res.json(user[0])
} else {
res.status(400).json('User not found')
}
});
});
app.get('/allusers', (req, res) => {
//const {id} = req.params;
//db.select('*').from('korisnici').then(data => {console.log(data)});
db.select('*').from('users').then(data => {res.json(data)});
});
//delete user
app.delete('/users/:name', (req, res) =>{
const email = req.params.email;
db.select()
.from('users').where({email: email}).del()
.then((users) =>{
db.select()
.from('login').where({email: email}).del()
.then(() => {
res.json(`user ${email} deleted`);
});
}).catch((error) => {
console.log(error);
});
});
//APPLICATION PORT
app.listen(3000, () =>{
console.log(('Port 3000 active'));
//res.send('Database active at port 3000')
});
【问题讨论】:
-
如有错误信息,请附上
-
这里是消息:ReferenceError: app is not defined at Object.
(c:\Users\AmlebC\Desktop\projekat\tempCodeRunnerFile.js:1:63) at Module._compile ( module.js:643:30) 在 Object.Module._extensions..js (module.js:654:10) 在 Module.load (module.js:556:32) 在 tryModuleLoad (module.js:499:12)在 Function.Module._load (module.js:491:3) 在 Function.Module.runMain (module.js:684:10) 在启动时 (bootstrap_node.js:187:16) 在 bootstrap_node.js:608:3 [完成] 在 4.492 秒内以 code=1 退出 -
这是我的 json 文件 { "name": "projekat", "version": "1.0.0", "description": "novi projekat", "main": "server.js" , "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "node server.js" }, "author": "", "license": " ISC”,“依赖项”:{“bcrypt”:“^3.0.6”,“body-parser”:“^1.18.3”,“cors”:“^2.8.5”,“express”:“^4.16 .4", "knex": "^0.16.5", "nodemon": "^1.18.11", "pg": "^7.10.0" } }
标签: javascript node.js postgresql express