微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我正在制作一个 Url 快捷方式我不明白如何向 app.get 发送请求我想获取表格数据并将其写入 html

如何解决我正在制作一个 Url 快捷方式我不明白如何向 app.get 发送请求我想获取表格数据并将其写入 html

我的服务器

我使用 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 举报,一经查实,本站将立刻删除。