Nodejs 공부기 2

선생님

생활코딩 Nodejs

배운 내용들

File Based Web App

  • Get / Post

    • Get : Using URL

    • Post : No URL

      • bodyParser
  • Web App Making

    • CR(UD)

    • Removing Overlapped Expression

  • File Uploading

    • Multer

ETC

  • Nodejs Auto-restart(supervisor module)

Real code

App.js(CR,File upload)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const express = require('express')
const bodyParser = require('body-parser')
const multer = require('multer')
const _storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads/')
},
filename: (req, file, cb) => {
cb(null, file.originalname + '-' + Date.now())
}
})
const upload = multer({ storage: _storage })
const fs = require('fs')
const app = express()
app.use('/user', express.static('uploads')) // uploads에 있는 파일들을 /user라는 url을통해서 서비스 하겠다. (유저가 갖고 갈 수 있게 함.)
app.use(bodyParser.urlencoded({ extended: false }))
app.set('views', './views')
app.set('view engine', 'pug')
app.get('/', (req, res) => {
res.send('hi fool!' )
})
app.get('/upload', (req, res) => {
res.render('upload')
})
// Middle ware first(processing)
app.post('/upload', upload.single('userfile'), (req, res) => {
console.log(req.file)
res.send('Uploaded : ' + req.file.filename)
})
app.get('/topic/new', (req, res) => {
fs.readdir('data', (err, files) => {
if (err) {
console.log(err)
res.status(500).send('Internal Server Error')
}
res.render('new', { topics: files})
})
})
app.get(['/topic', '/topic/:id'], (req, res) => {
fs.readdir('data', (err, files) => {
if (err) {
console.log(err)
res.status(500).send('Internal Server Error')
}
const id = req.params.id
if (id) {
fs.readFile(`data/${id}`, 'utf8', (err, data) => {
if (err) {
console.log(err)
res.status(500).send('Internal Server Error')
}
res.render('view', { title: id, topics: files, description: data})
})
} else {
res.render('view', { topics: files, title: 'Welcome', description: 'Hello, JavaScript for server.' })
}
})
})
app.post('/topic', (req, res) => {
const title = req.body.title
const description = req.body.description
fs.writeFile(`data/${title}`,description, (err) => {
if (err) {
res.status(500).send('Internal Server Error!')
}
res.redirect('/topic')
})
})
app.listen(3000, () => {
console.log('Connected at 3000port!')
})
Share