したがって、使用する言語は、ご使用のサーバーがサポートしている言語、または自分でインストールできる言語によって異なります。これをテストするためにコンピュータ上でサーバーを稼働させたい場合は、使用する言語をインストールして、コンピュータ上でサーバーとして実行する方法を見つけるだけで済みます。
任意のデータベース(postgres、mongodb)を使用して、あなたのURLを保存することができます。このプロジェクトでメモリを使用しても問題ない場合は、辞書を使用してランダムなURLを格納できます。
これは、Node.jsを使用してこれを行う方法です。このソリューションでは、ポート9898にサーバーを作成し、辞書を使用してメモリ内のデータベースを作成します。エントリがある場合、データベースは次のようになります。
{
"uuid1": { id: "uuid1", urls: ["https://www.google.com", "https://youtube.com"] },
"uuid2": { id: "uuid2", urls: ["https://www.google.com", "https://youtube.com"] }
}
uuid1とuuid2は実際の一意のIDに置き換えられます。
この解決策では、クライアントがjsonオブジェクトを送信することを期待しています。
解決策はexpressjsとbody-parserパッケージに依存します。実際のコードは私のコンピュータでテストします。あなたは決して設定をした場合
const express = require('express')
const bodyParser = require('body-parser');
const app = express()
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
const port = 9898
const host = `http://localhost:${port}`
// you would replace this with an actual database
const db = {}
// to generate a uuid
// from: https://stackoverflow.com/a/2117523
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
function success(message) {
return JSON.stringify({
success: message
})
}
function error(message) {
return JSON.stringify({
error: message
})
}
// assuming body looks like this { urls: ['www.google.com', 'www.gmail.com'] }
app.post('/gen', (req, res) => {
const gen = req.body
// check if the json in the request has the list of urls
if(!(gen && gen.urls && gen.urls.length > 0)) {
// if not then log the body and return a json object with the error object
console.log('body', req.body)
return res.send(error('I cannot find the list of urls in json body.'))
}
// generate a uuid
const id = uuidv4()
// save the urls with the new id
db[id] = {
id: id,
urls: gen.urls
}
// respond with the url that the user should use
res.send(success(host + '/' + id))
})
app.get('/:id', (req, res) => {
// get the id paramater from the request
const id = req.params.id
// check if that id was saved, if not send an error message
if(!db[id])
return res.send(error('that url does not exist'))
// grab a random element from the list of urls
const urls = db[id].urls
const url = urls[Math.floor(Math.random() * urls.length)]
// redirect the user to the random url
res.redirect(url)
})
// start the server
app.listen(port,() => {
console.log('listening on port: '+port);
})
/*
example request:
curl -X POST -H "Content-Type: application/json" -d '{"urls": ["https://www.google.com", "https://www.yahoo.com", "https://www.youtube.com"]}' localhost:9898/gen
after doing this you grab the url generated and paste into your browser.
for example when I ran this it generated: http://localhost:9898/aa582177-4ab5-4ede-99c5-a06b43976c12
*/
このページ前nodejsプロジェクトがスピードにあなたを得ることができます:https://docs.npmjs.com/getting-started/installing-node は後nodejsとNPMをインストールします。プロジェクトのフォルダを作成します。コマンドラインで、このディレクトリに移動します。セットアップするためのプロジェクトと二つのパッケージをインストールし、書き込み:
npm init -y
npm install express body-parser
を
それはそのフォルダにindex.jsという名前のファイルを作成し行った後。次にコードを貼り付けます。返すことを次に
curl -X POST -H "Content-Type: application/json" -d '{"urls": ["https://www.google.com", "https://www.yahoo.com", "https://www.youtube.com"]}' localhost:9898/gen
:あなたはこの例で要求を使用することができ、あなたのMacOSやLinuxの場合
node index.js
、またはWindows上でカールを使用する方法があります。次に、このコマンドのサーバーの使用を実行しますあなたのためにあなたのためのURL。あなたがブラウザに生成されたURLを貼り付けますと、それは、リスト内のURLのいずれかにあなたを再ルーティングする必要があり
http://localhost:9898/aa582177-4ab5-4ede-99c5-a06b43976c12
:たとえば、私のためには、これを生成しました。
これは素晴らしい説明です。ありがとうございます。 –