私はURLの短縮文字を書いていますが、いくつか問題があります。データベーステーブルの値にリダイレクト
したがって、ユーザーが要求を送信するとき、http://localhost:3000/1言って、私はそれが私のデータベースにある1のIDを持つ列original_urlの下に格納されたURLにリダイレクトします。ここで
私はこれを達成するために書いた関数である。
// :id will match anything after the/in the url, and set it as the req.params.id value
app.get('/:id', function (req, res) {
console.log("hi you're about to match url id to database id");
//get a prostgres client from the connection pool
pg.connect(connectionString, (err, client, done) => {
//handle connection errors
if (err) {
done();
console.log(err);
return res.status(500).json({ success: false, data: err });
}
//match id in database to id from query in url
const query = client.query("SELECT * FROM items WHERE id =" + req.params.id);
console.log("successfully matched database id value to id value in url");
console.log(req.query);
res.redirect(req.query);
});
});
問題は、私は、正しいIDと一致した後、私は私の列に格納されているURLにユーザーをリダイレクトする方法がわからない、ではoriginal_urlには、リダイレクト先のURLが含まれています。
入力したIDに対応するオリジナルID番号の値を取得するにはどうすればよいですか?あなたは、NPM(NPM I --save PG)からノード-postgresのパッケージを使用していると仮定すると、