
const express = require('express')
const app = express()
app.listen(8080, () => {
console.log('http://localhost:8080 에서 서버 실행중')
})
app.get('/', (요청, 응답) => {
응답.send('반갑다')
})
app.get('/news', (요청, 응답) => {
응답.send('오늘비옴')
})
app.get('/news', (요청, 응답) => {
응답.sendFile(__dirname + 'index.html')
})
📍 __dirname 은 현재의 파일(file)이 위치한 폴더(directory)의 절대경로(absolute path)를 알려준다.
폴더를 생성하고 그 폴더를 서버파일에 app.use라는 문법으로 등록해두어야 폴더 안에 있는 static 파일들(css, js, 이미지 파일들)을 사용할 수 있다.
app.use(express.static(__dirname + '/public'));
<link href="/main.css" rel="stylesheet">


npm install mongodb@5
const { MongoClient } = require('mongodb')
let db
const url = 'mongodb사이트에 있던 접속 URL'
new MongoClient(url).connect().then((client)=>{
console.log('DB연결성공')
db = client.db('forum')
app.listen(8080, () => {
console.log('http://localhost:8080 에서 서버 실행중')
})
}).catch((err)=>{
console.log(err)
})

app.get("/news", () => { db.collection("콜렉션명").insertOne({보낼key값:보낼내용}
} )
app.get("/news", () => {
let result = db.collection("콜렉션명").find().toArray()
console.log(result)
} )