[nodejs]EJS사용하기/Using EJS – 1

브라우저의 웹페이지에서 바로 물리적인 서버에접근하는것이 기본적으로는 불가능합니다.
그래서 asp,php,jsp같은 Server side script가 만들어졌습니다.
asp,php,jsp를 사용해보신 분들이 EJS(Embedded JavaScript templates for node)를 사용하면 이해가 쉽습니다.
기존의 server-side script언어와 매우 유사한 점이 많습니다.

It is basically impossible to access the physical server directly from the browser’s web page.
So, server side scripts such as asp, php, and jsp were created.
For those who have experience with asp, php, and jsp, it is easy to understand using EJS (Embedded JavaScript templates for node).
It has many similarities to existing server-side script languages.

React와 비교해보자면 React는 1페이지 어플리케이션으로 이벤트 발생시 컴포넌트를 교체하는 방법으로 페이지를 구현한다면 EJS는 파일을 include하거나 node server 페이지에서 변수,배열,객체등을 ejs파일로 전달합니다.

Compared to React, React is a one-page application that implements the page by replacing components when an event occurs, but EJS includes files or transfers variables, arrays, and objects from the node server page to the ejs file.

작업환경은 저 같은 경우 윈도우pc+visual studio code로 작업하고 oci linux server로 파일을 업로드 합니다.
아래는 윈도우환경에서의 설치 방법이지만 리눅스환경에서도 동일한 명령어를 사용하시면 됩니다.
ftp로 파일업로드하는 부분은 생략했습니다.

In my case, I upload files to the Oci Linux server using PC+visual studio code.
Below is the installation method in a Windows environment, but you can use the same command in a Linux environment.
I omitted the part about uploading files via ftp.

아래는 간단히 EJS를 사용하는 방법입니다.
Below is a simple way to use EJS.

1.프로젝트시작(Project start)
– npm init 명령어로 nodejs프로젝트를 시작해서 package.json파일을 생성합니다.
– 그냥 질문에 모두 엔터치면 디폴트 값으로 프로젝트 시작합니다.

Start the nodejs project with the npm init command and create a package.json file.
Just press Enter on all questions and the project will start with default values.

2.모듈설치(Module installation)
– npm install express path ejs –save 명령어로 ejs모듈 및 express,path모듈을 설치합니다
– ejs만 설치할경우 npm install ejs –save 명령어를 사용하시면 됩니다.

Install the ejs module and express, path module with the npm install express path ejs –save command.
If you want to install only ejs, use the npm install ejs –save command.

3.node 서버 파일 생성(Create node server file)
– server.js파일을 생성합니다. Create a server.js file.

4.public디렉토리 만들고 아래에 test.ejs파일을 생성합니다.
Create a public directory and create a test.ejs file below.
– 루트 디렉토리(root directory)

-test.ejs파일생성 (Create a test.ejs file)

5.node서버 시작 및 ejs파일 테스트(node server test and ejs file test)
– node server start(linux)

– 브라우저에서 ejs파일 실행(run ejs file in browser)

Leave a Reply

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