0
0

【Node.js】Expressを使ってウェブアプリを作る

Posted at

Node.jsで
さくっとウェブアプリで試したいとき
毎回、えーとって悩むので自分用備忘録

$ npm init
$ npm i express

作成された package.json

package.json
{
  "name": "express-test",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2"
  }

ルートディレクトリにmainに指定した app.js ファイルを作成

app.js
var http = require("http");
var express = require("express");

var app = express();

app.get("/", function (req, res) {
  return res.send("Hello World");
});

// ルーティング
app.get("/hoge", function (req, res) {  
  return res.send("Hoge");
});

var server = http.createServer(app);
server.listen(3000);

app.js を実行

node app.js

http://localhost:3000/ にアクセスすると
「Hello World」
と表示されます。

ルーティングにより
http://localhost:3000/hoge にアクセスすると
「Hoge」
と表示されます。

以上です。

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