nodejs3 – HTML, EJS 파일 불러오기 / Importing HTML,EJS

package-lock.json파일은 자동생성되는 파일이라서 따로 올리지 않겠습니다.
The package-lock.json file is an automatically generated file, so I will not upload it separately.

server.js

/*---------------------------------------------------------------------------

- NODEJS-3 : router
- 2025.02.

---------------------------------------------------------------------------*/

const path = require('path'); // npm install path
const express = require('express'); // npm install express
const app = express();
const port = 3000; 

/*---------------------------------------------------------------------------
  start route
---------------------------------------------------------------------------*/

// print: res.send('Hello, World!');
// file: res.sendFile(path.join(__dirname, "/", "index1.html"));

app.get('/', (req, res) => {
  //res.send('Hello, World!');
  res.sendFile(path.join(__dirname, "/", "index1.html"));

  console.log("log:index1.html");
  console.log(`log: filename : ${__filename}`); // server.js
  console.log(`log: path:${__dirname}`);
});

/*---------------------------------------------------------------------------
  Start Server
---------------------------------------------------------------------------*/

// Start the server
app.listen(port, () => {
  console.log(`Server listening at http://localhost:${port}`);
});

server2.js

/*---------------------------------------------------------------------------

- NODEJS-3 : router2
- 2025.02.

---------------------------------------------------------------------------*/

// module : https://www.npmjs.com
const ejs = require('ejs');         // https://ejs.co
const path = require('path');       // https://nodejs.org/docs/latest/api/path.html
const express = require('express'); // https://expressjs.com/

const app = express();
const port = 3000; 

// ejs
app.set('view engine','ejs');
app.set('views','./public');


/*---------------------------------------------------------------------------
  Start route
---------------------------------------------------------------------------*/

// print: res.send('Hello, World!');
// file: res.sendFile(path.join(__dirname, "/", "index1.html"));
// ejs: res.render('view1')

app.get('/', (req, res) => {
  //res.send('Hello, World!');
  res.sendFile(path.join(__dirname, "/", "index1.html"));

  console.log("log:index1.html");
  console.log(`log: filename : ${__filename}`); // server.js
  console.log(`log: path:${__dirname}`); // directory path
});

app.get('/view1', (req, res) => {
  res.render('view1'); // ejs file
});


/*---------------------------------------------------------------------------
  Start Server
---------------------------------------------------------------------------*/

// Start the server
app.listen(port, () => {
  console.log(`Server listening at http://localhost:${port}`);
});

package.json

{
  "name": "nodejs3-1",
  "version": "1.0.0",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "ejs": "^3.1.10",
    "express": "^4.21.2",
    "path": "^0.12.7"
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *