Help us understand the problem. What is going on with this article?

node.js、express、ejsでHello Worldするコード

More than 1 year has passed since last update.

記載の動機

node.jsでコーディングをするようになったが、必須で記述する部分などを覚えきれないので防備録として記述しました。

前提

・node.jsがインストールされていること node.js 公式サイト
・Windows7 64bitのPCで実行

コードとコマンド

cmd
app> npm init
app> npm install express ejs --save
directory
app
|-/views
   |-landing.ejs
|-/node_modules **自動で生成される
|-app.js
|-package.json **自動で生成される
|-package-lock.json **自動で生成される
app.js
var express = require("express");
var app = express();

//viewエンジンをejsであることを設定
app.set("view engine", "ejs");

app.get('/', function(req, res){
    const message = "Hello World!";
    res.render("landing", {message: message});
});

app.listen(3000, () => console.log('app listening on port 3000!'))
landing.ejs
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Page Title</title>
</head>
<body>
    <h1>Landing page</h1>
    <!-- コードから渡されたデータを表示-->
    <p><%= message %></p>
</body>
</html>
起動~cmd
app> node app.js

ブラウザで以下にアクセスする。
http://localhost:3000/

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした