0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Windows11 に Node.js インストール, JavaScript実行, React 環境構築

Last updated at Posted at 2023-03-15

Windows11 に Node.js インストールして JavaScript 実行、React環境構築をやってみる
やることは大雑把にこんあところ

  • Node.jsインストール
  • Node.js で JavaScript実行(簡単なHTTPサーバたてる)
  • React環境構築

Node.js インストール(node:v18.15.0, npm:v9.5.0)

  1. Node.js インストーラー取得(LTSの方を選択)
    https://nodejs.org/
  2. インストーラ実行
    画面に従い進める、許可などは確認して同意、はいを選択
    途中でコマンドプロンプト、PowerShell 上がってくるのも画面に従いキー入力
    Warning とか出ても気にしないでOK
  3. インストール確認
    コマンドプロンプト or Powrshell で以下を入力してバージョン確認
    # Node
    node --version
    # Node Package Manager
    npm --version
    

Node.js で JavaScript実行(HTTPサーバ)

  1. 適当なフォルダを作る
    C:\nodework
  2. フォルダの中に JavaScript index.js を作る
    C:\nodework\index.js
  3. index.js に以下のコードを入力
    index.js
    // モジュールインポート
    const http = require("http");
    
    // HTTPサーバ設定
    http.createServer((request, response) => {
        switch (request.url) {
            case '/json':
                response.writeHead(200, { "content-type": "application/json", "charset": "utf-8" });
                response.write("{ text : Hello World }");
                response.end();
                break;
            case '/html':
                response.writeHead(200, { "content-type": "text/html", "charset": "utf-8" });
                response.write("<!doctype html><html><body><h1>Hello World</h1></body></html>");
                response.end();
                break;
            default:
                response.writeHead(200, { "content-type": "text/plain" });
                response.write("Hello World");
                response.end();
        }
    }).listen(8888);
    
  4. コマンドプロンプト or Powrshell で作ったフォルダに移動
    cd C:\nodework
    
  5. コマンドプロンプト or Powrshell で Node 使って JavaScript 実行
    以下のコマンド実行で Node から JavaScript が実行される
    node index.js
    # 実行すると Windows セキュリティの警告が出るときは気にせずキャンセルでOK
    # 他のPCとかデバイスから接続させたいときはアクセス許可するようにする
    
  6. 実行したやつにアクセス
    以下にブラウザからアクセスで Hello World が表示されればOK
    http://localhost:8888/
    http://localhost:8888/json
    http://localhost:8888/html

React環境構築(create-react-app:v5.0.1)

公式の Create a New React App を参考にやってみる

  1. 適当なフォルダを作る
    C:\reactwork
  2. コマンドプロンプト or Powrshell で作ったフォルダに移動
    cd C:\reactwork
    
  3. create-react-app よりプロジェクト名を指定して作成
    npx create-react-app my-app
    # my-app のフォルダが作られて開発するプロジェクトになる
    # create-react-app のインストールするか聞かれたら y で続ける
    
  4. 作ったプロジェクトに移動
    cd my-app
    
  5. 作ったプロジェクトを実行
    npm start
    
  6. ブラウザで以下が上がってくればOK
    http://localhost:3000/
    image.png
0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?