💻STUDY/BACKEND STUDY

[Node.js] 7주차

coldNoodlePigeon 2022. 5. 22.

Fs Module 

Sync 

const {readFileSync, writeFileSync} = require('fs');
const first = readFileSync('./content/first.txt', 'utf8');
const second = readFileSync('./content/second.txt', 'utf8');
console.log(fisrt, second)
// 출력:
// Hello this is first text file Hello this is second text file
writeFileSync(
	('./content/result-sync.txt'),
	`Here is the result : ${first}, ${second}`,
	{ flag : 'a' } // 해당 플래그 추가 시 새로운 값(새로운 줄) 만들어짐
)

 

Async 

 

const {readFileSync, writeFileSync} = require('fs');
readFile('./content/first.txt','utf8', (err,result) => {
if (err) {
console.log(err)
return
}
// console.log(result)
// 출력: Hello this is first text file
const first = result;
readFile('./content/second.txt','utf8', (err,result) => {
    if (err) {
    	console.log(err)
    	return
    }
    const second = result
    writeFile(
        './content/result-async.txt',
        `Here is the result : ${first}, ${second}`
    ,(err,result)=>{
    if(err){
        console.log(err)
        return;
    }
        console.log(result)
})
})
})

 

 

Http Module 

const http = require('http');
const server = http.createServer((req,res) => {
	if(req.url === '/'){
		res.end('Welcome to our homepage')
    }
    if(req.url === '/about'){
		res.end('Here is our short history')
	}
    res.end(`
    <h1>Oops!</h1>
    <p>We can't seem to find the page you are looking for</p>
    <a href="/">back home</a>
    `) 
})

 

 

First Package 

{
    "name": "nodetutorial",
    "version": "1.0.0",
    "description": "",
    "main": "1-intro.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
},
    "author": "",
    "license": "ISC"
}
// node_modules라는 폴더가 만들어짐 - 하위: /lodash, /packagelock.json, || /bootstrap, /jquery
const _ = require('lodash')
const items = [1, [2, [3, [4]]]]
const newItems = _.flattenDeep(items)
console.log(newItems) // [1, 2, 3, 4]

 

 

**정리가 미흡한건 차차 채워넣겠습니다..!**

'💻STUDY > BACKEND STUDY' 카테고리의 다른 글

[Node.js] 웹 API 서버 만들기  (0) 2022.07.28
[Node.js] MySQL 데이터베이스  (0) 2022.07.26
[Node.js] 6주차  (0) 2022.05.15
[Node.js] 5주차  (0) 2022.05.08
[Node.js] 4주차  (0) 2022.05.01

댓글