问题描述
我的服务器
我使用 postgresql 部署了数据库。 数据写入数据库。它有效。
const path = require('path')
const express = require('express')
const bodyParser = require('body-parser')
const { customAlphabet,nanoid } = require('nanoid')
const app = express()
const router = express.Router()
const jsonParser = bodyParser.json()
const urlencodedParser = bodyParser.urlencoded({ extended: false })
const pgp = require('pg-promise')()
const dbAuth = {
host: 'localhost',port: 5432,database: 'test_db',user: 'postgres',password: 'xxxx',}
const db = pgp(dbAuth)
app.post('/api',jsonParser,async (req,res) => {
console.log(req.body.url)
try {
let result = await db.none('INSERT INTO urls(url,id) VALUES($/url/,$/id/)',{
url: req.body.url,id: nanoid(),})
console.log(result)
} catch (error) {
console.log(error)
}
res.end(JSON.stringify({
status: 'ok',shortUrl: 'tempvalue',}))
})
我这里有一个错误,我不知道如何解决 您应该如何处理 get 请求?
app.get('/:id',function (req,res) {
db.one('select * from urls where id = $1',req.params.id)
.then(function (urls) {
res.redirect(urls.url);
})
.catch(function () {
res.redirect('/');
});
});
这里一切正常
app.use(express.urlencoded({ extended: false }))
app.use(express.static('public'))
app.listen(process.env.PORT || 5000)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)