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 3 years have passed since last update.

Node.jsでサーバーと通信

Posted at

今回はNode.jsを利用してサーバーと通信をしていきたいと思います。Node.jsはインストールされているものとして話を進めていきたいと思います。
まずは、mkdir "フォルダ名"でフォルダを作成し、次に cd "フォルダ名"で作成したフォルダに移動します。そこで npm init を実行してフォルダに必要なものをインストールします。
続いて、npm install expressを実行してExpressモジュールを導入します。そしたら、javascriptファイル(今回はindex.js)を作成し、以下のコードを入力します。

index.js
var express = require('express');
var app = express();
app.listen(8080);

app.get('/',function(req,res){
   res.send('Hello world!\n');
});

保存したら node index.jsを実行し、Webブラウザから http://localhost:8080にアクセスすると"Hello world!"と表示されてます。
ついでに、以下のコードを追加して

index.js
app.get('/other',function(req,res){
   res.send('こんにちは\n');
});

http://localhost:8080/otherにアクセスすると
"こんにちは"と表示されてます。

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?