개발/node.js
[node.js] node.js CRUD기능 예제
개발햄
2019. 2. 20. 21:31
https://github.com/leehanbi/nodeStudy/
lib/author.js
lib/topic.js
lib/db.js
참고
main.js
Executable File 56 lines (55 sloc) 1.85 KB
var http = require('http'); | |
var url = require('url'); | |
var topic = require('./lib/topic.js'); | |
var author = require('./lib/author.js'); | |
var app = http.createServer(function(request,response){ | |
var _url = request.url; // queryData를 통해서 전송한 값을 받을 수 있음 | |
var queryData = url.parse(_url, true).query; | |
var pathname = url.parse(_url, true).pathname; | |
// 메인화면 | |
if(pathname === '/'){ | |
if(queryData.id === undefined){ | |
topic.main(response); | |
} else { | |
topic.main_sub(queryData,response); | |
} | |
// 글만들기 | |
}else if(pathname === '/create'){ | |
topic.create(response); | |
// 글만들기 작성 | |
} else if(pathname === '/create_process'){ | |
topic.create_process(request, response); | |
// 글수정 | |
} else if(pathname === '/update'){ | |
topic.update(response, queryData); | |
// 글수정 업뎃 | |
} else if(pathname === '/update_process'){ | |
topic.update_process(request ,response); | |
// 글삭제 | |
} else if(pathname === '/delete_process'){ | |
topic.delete_process(request ,response); | |
// author 불러오기 | |
} else if(pathname === '/author'){ | |
author.author(response); | |
// author 목록 작성 | |
} else if(pathname === '/author_create'){ | |
author.author_create(response); | |
// author 목록 인설트 | |
} else if(pathname === '/author_create_process'){ | |
author.author_create_process(request ,response); | |
// author 수정 | |
} else if(pathname === '/author_update'){ | |
author.author_update(queryData, response); | |
// author 수정 업뎃 | |
} else if(pathname === '/author_update_process'){ | |
author.author_update_process(request ,response); | |
// author 목록 삭제 | |
} else if(pathname === '/author_delete'){ | |
author.author_delete(request, response); | |
// 예외처리 | |
} else { | |
response.writeHead(404); | |
response.end('Not found'); | |
} | |
}); | |
app.listen(3000); |
db.js
11 lines (9 sloc) 184 Bytes
var mysql = require('mysql'); | |
// 자신의 주소 작성 | |
var db = mysql.createConnection({ | |
host : '', | |
user : '', | |
password : '', | |
database : '' | |
}); | |
db.connect(); | |
module.exports= db; |
author.js
197 lines (188 sloc) 6.16 KB
var template = require('../lib/template.js'); | |
var qs = require('querystring'); | |
var db = require('./db.js'); | |
var sanitize = require('sanitize-html'); | |
exports.author = function(response){ | |
db.query('SELECT * FROM topic', (err,topics)=> { | |
if(err){ | |
throw err; | |
} | |
db.query(`SELECT * FROM author`,(err2,authors)=>{ | |
if(err2){ | |
throw err2; | |
} | |
var title = 'author_data'; | |
var list = template.list(topics); | |
var html = template.HTML(title, list, | |
` | |
${template.author_table(authors)} | |
<form action="/author_create" method="post"> | |
<button>create</button> | |
</form> | |
<style> | |
table{ | |
WIDTH: 500px; | |
border-collapse: collapse; | |
} | |
td { | |
WIDTH: 500px; | |
border : 1px solid black; | |
} | |
</style> | |
`, | |
`<a href="/">back</a>` | |
); | |
response.writeHead(200); | |
response.end(html); | |
}); | |
}); | |
} | |
exports.author_create = function(response){ | |
db.query('SELECT * FROM topic', (err,topics)=> { | |
if(err){ | |
throw err; | |
} | |
db.query(`SELECT * FROM author`,(err2,authors)=>{ | |
if(err2){ | |
throw err2; | |
} | |
var title = 'author_data'; | |
var list = template.list(topics); | |
var html = template.HTML(title, list, | |
` | |
${template.author_table(authors)} | |
<form action="/author_create_process" method="post"> | |
<table> | |
<tr> | |
<td>글쓴이</td> | |
<td>프로필</td> | |
</tr> | |
<tr> | |
<td><input type="text" name="name"></td> | |
<td><input type="text" name="profile"></td> | |
</tr> | |
</table> | |
<button>create</button> | |
</form> | |
<style> | |
table{ | |
WIDTH: 500px; | |
border-collapse: collapse; | |
} | |
td { | |
WIDTH: 500px; | |
border : 1px solid black; | |
} | |
</style> | |
`, | |
`<a href="/">back</a>` | |
); | |
response.writeHead(200); | |
response.end(html); | |
}); | |
}); | |
} | |
exports.author_create_process = function(request ,response){ | |
var body = ''; | |
request.on('data', function(data){ | |
body = body + data; | |
}); | |
request.on('end', function(){ | |
var post = qs.parse(body); | |
var name = sanitize(post.name); | |
var profile = sanitize(post.profile); | |
db.query(` | |
INSERT INTO author (name, profile) | |
VALUES (?,?)`, [name, profile],(err,result)=> { | |
if(err){ | |
throw err; | |
} | |
response.writeHead(302, {Location: `/author`}); | |
response.end(); | |
}); | |
}); | |
} | |
exports.author_update = function(queryData,response){ | |
db.query('SELECT * FROM topic', (err,topics)=> { | |
if(err){ | |
throw err; | |
} | |
db.query(`SELECT * FROM author`,(err2,authors)=>{ | |
if(err2){ | |
throw err2; | |
} | |
db.query(`SELECT * FROM author WHERE id=?`,[queryData.id],(err3,author)=>{ | |
if(err3){ | |
throw err3; | |
} | |
var title = 'author_data'; | |
var list = template.list(topics); | |
var html = template.HTML(title, list, | |
` | |
${template.author_table(authors)} | |
<form action="/author_update_process" method="post"> | |
<table> | |
<tr> | |
<td>글쓴이</td> | |
<td>프로필</td> | |
</tr> | |
<tr> | |
<input type="hidden" name="id" value ="${queryData.id}"> | |
<td><input type="text" name="name" value="${author[0].name}"></td> | |
<td><input type="text" name="profile" value="${author[0].profile}"></td> | |
</tr> | |
</table> | |
<button>update</button> | |
</form> | |
<style> | |
table{ | |
border-collapse: collapse; | |
} | |
td { | |
border : 1px solid black; | |
} | |
</style> | |
`, | |
`<a href="/">back</a>` | |
); | |
response.writeHead(200); | |
response.end(html); | |
}); | |
}); | |
}); | |
} | |
exports.author_update_process = function(request,response){ | |
var body = ''; | |
request.on('data', function(data){ | |
body = body + data; | |
}); | |
request.on('end', function(){ | |
var post = qs.parse(body); | |
var name = sanitize(post.name); | |
var profile = sanitize(post.profile); | |
console.log(post.id); | |
db.query('UPDATE author SET name=?, profile=? WHERE id=?',[name, profile, post.id],(err,result)=>{ | |
if(err){ | |
throw err; | |
} | |
response.writeHead(302, {Location: `/author`}); | |
response.end(); | |
}); | |
}); | |
} | |
exports.author_delete = function(request,response){ | |
var body = ''; | |
request.on('data', function(data){ | |
body = body + data; | |
}); | |
request.on('end', function(){ | |
var post = qs.parse(body); | |
db.query('DELETE FROM author WHERE id=?',[post.id],(err,result)=>{ | |
if(err){ | |
throw err; | |
} | |
response.writeHead(302, {Location: `/author`}); | |
response.end(); | |
}); | |
}); | |
} |
topic.js
155 lines (147 sloc) 5.01 KB
var template = require('../lib/template.js'); | |
var qs = require('querystring'); | |
var sanitize = require('sanitize-html'); | |
var db = require('./db.js'); | |
exports.main = function(response){ | |
db.query('SELECT * FROM topic', (err,topics)=> { | |
var title = 'Welcome'; | |
var description = 'Hello, Node.js'; | |
var list = template.list(topics); | |
var html = template.HTML(title, list, | |
`<h2>${title}</h2>${description}`, | |
`<a href="/create">create</a>` | |
); | |
response.writeHead(200); | |
response.end(html); | |
}); | |
} | |
exports.main_sub= function(queryData,response){ | |
db.query('SELECT * FROM topic', (err,topics)=> { | |
if(err){ | |
throw err; | |
} | |
db.query(`SELECT * FROM topic LEFT JOIN AUTHOR ON topic.author_id = AUTHOR.id WHERE topic.id=?`, [queryData.id], (err2,topic)=>{ | |
if(err2){ | |
throw err2 | |
} | |
var title = topic[0].title; | |
var description = topic[0].description; | |
var list = template.list(topics); | |
var html = template.HTML(title, list, | |
`<h2>${title}</h2>${description}`, | |
` <a href="/create">create</a> | |
<a href="/update?id=${queryData.id}">update</a> | |
<form action="delete_process" method="post"> | |
<input type="hidden" name="id" value="${queryData.id}"> | |
<input type="submit" value="delete"> | |
</form>` | |
); | |
response.writeHead(200); | |
response.end(html); | |
}) | |
}); | |
} | |
exports.create = function(response){ | |
db.query('SELECT * FROM topic', (err,topics)=> { | |
var title = 'Welcome'; | |
var list = template.list(topics); | |
var html = template.HTML(title, list, ` | |
<form action="/create_process" method="post"> | |
<p><input type="text" name="title" placeholder="title"></p> | |
<p> | |
<textarea name="description" placeholder="description"></textarea> | |
</p> | |
<p> | |
<input type="submit"> | |
</p> | |
</form> | |
`, ''); | |
response.writeHead(200); | |
response.end(html); | |
}); | |
} | |
exports.create_process = function(request, response){ | |
var body = ''; | |
request.on('data', function(data){ | |
body = body + data; | |
}); | |
request.on('end', function(){ | |
var post = qs.parse(body); | |
var title = sanitize(post.title); | |
var description = sanitize(post.description); | |
db.query(` | |
INSERT INTO topic (title, description, created, author_id) | |
VALUES (?,?,NOW(),?)`, [title, description, 1],(err,result)=> { | |
if(err){ | |
throw err; | |
} | |
response.writeHead(302, {Location: `/?id=${result.insertId}`}); | |
response.end(); | |
}); | |
}); | |
} | |
exports.update = function(response, queryData){ | |
db.query('SELECT * FROM topic', (err,topics)=> { | |
if(err){ | |
throw err; | |
} | |
console.log(queryData.id); | |
db.query('SELECT * FROM topic WHERE id =?', [queryData.id], (err2,topic)=> { | |
if(err2){ | |
throw err2; | |
} | |
var list = template.list(topics); | |
var html = template.HTML(topic[0].title, list, | |
` | |
<form action="/update_process" method="post"> | |
<input type="hidden" name="id" value="${topic[0].id}"> | |
<p><input type="text" name="title" placeholder="title" value="${topic[0].title}"></p> | |
<p> | |
<textarea name="description" placeholder="description">${topic[0].description}</textarea> | |
</p> | |
<p> | |
<input type="submit"> | |
</p> | |
</form> | |
`, | |
`<a href="/create">create</a> <a href="/update?id=${topic[0].id}">update</a>` | |
); | |
response.writeHead(200); | |
response.end(html); | |
}); | |
}); | |
} | |
exports.update_process = function(request ,response){ | |
var body = ''; | |
request.on('data', function(data){ | |
body = body + data; | |
}); | |
request.on('end', function(){ | |
var post = qs.parse(body); | |
var title = sanitize(post.title); | |
var description = sanitize(post.description); | |
db.query('UPDATE topic SET title=?, description=?, author_id=1 WHERE id=?',[title, description, post.id],(err,result)=>{ | |
if(err){ | |
throw post.id; | |
} | |
response.writeHead(302, {Location: `/?id=${post.id}`}); | |
response.end(); | |
}); | |
}); | |
} | |
exports.delete_process = function(request ,response){ | |
var body = ''; | |
request.on('data', function(data){ | |
body = body + data; | |
}); | |
request.on('end', function(){ | |
var post = qs.parse(body); | |
db.query('DELETE FROM topic WHERE id =? ',[post.id],(err,result)=>{ | |
if(err){ | |
throw err; | |
} | |
response.writeHead(302, {Location: `/`}); | |
response.end(); | |
}); | |
}); | |
} |