LoginSignup
8
9

More than 3 years have passed since last update.

リダイレクト処理を実装

Last updated at Posted at 2018-08-08

リダイレクトとは?

リダイレクトとは、サイトにアクセスしたユーザーをアクセスしたユーザーをアクセスしたURIとは別のURIにアクセスさせること。

index.js

'use strict';
const http = require('http');
const server = http.createServer((req, res) => {
  res.writeHead(302, {
    'Location': 'https://www.google.co.jp/'
  });
  res.end();
});
const port = 8000;
server.listen(port, () => {
  console.info('Listening on ' + port);
});

解説

res.writeHead(302, {
    'Location': 'https://www.google.co.jp/'
});

以上がリダイレクトを実行する処理。
ステータスコードの302-Foundは「Locationという項目のヘッダで指定されたサイトに内容を発見した」ということを表すステータスコード。
node index.jsを実行し、うまくGoogleに飛べばおけ!

※2020年1月更新

MENTAでコードレビューやプログラミング勉強相談を行っています!

お気軽にメッセージを下さい!
プランはこちらになります!

8
9
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9