본문 바로가기

server

node js server 첫 스타트!!

1. npm init ( package.json )파일 만들기 위해 사용하는 명령어

    

 

2.entry point 에 이름을 만들어주어야 함!!!!

package name: (filtereddatapractice) 
version: (1.0.0) 
description: 
entry point: (index.js) server.js
test command: 
git repository: 
keywords: 
author:    

3. npm install express( 서버 쉽게 쓰기 위한 라이브러리 )

 

 

4.server.js 에 서버 만들고 확인하기 

//라이브러리 불러오기
const express= require('express')
라이브러리 넣기 
const app= express()


//8080 이름에 서버 오픈
app.listen(8080,()=>{

	console.log( 'listening on 8080')
})

5. get 요청하기 : 내가 어떤 url 를 입력하면 (포트이름) 보여주기 

 

app.get('/pet', (request, response)=>{

	response.send('펫 용품입니다')

})

 

* 매 번 server의 내용이 바뀔 때마다 서버를 껏다 킬 수 없다. 그렇기 때문에


 

1.터미널 켜서 npm install -g nodemon 입력

2. nodemon server.js 라고 입력

 

 

 

6. get 요청 : html File을 보내고 싶을 때는 

 

app.get('/', function(요청, 응답) { 
  응답.sendFile(__dirname +'/index.html')
});

7. 내가 보낸 데이터를 잘 읽기 위해서 설치 및 선언( 설치는 2021년도 이후로 express 라이브러리에 설치되어 있기 때문에 안해도 됨)

 

const bodyParser= require('body-parser')
app.use(bodyParser.urlencoded({extended: true}))


//최근에는 express library에 설치되어있기 때문에 선언만 진행

app.use(express.urlencoded({extended: true}))

8. /add라는 행동을 사용자가 진행했을 때  서버야 내 데이터를 저장해줘.  

<form action="/add" method="POST">
app.post('/add', function(요청, 응답){
  console.log(요청.body);
  응답.send('전송완료')
});

name 속성 이용하여 데이터 값 넣어주기 그래야 어떤 input인지 찾을 수 있음.

    <input type="text" class="form-control" name="title">

 

'server' 카테고리의 다른 글

http 상태코드  (0) 2021.11.03
회원관리 시스템  (0) 2021.11.03
HTTP API URL 설계  (0) 2021.11.02
URI URL 그리고 통신 동작 원리  (0) 2021.11.02
HTTP 흐름으로 이해하기  (0) 2021.11.02