Skip to content

Instantly share code, notes, and snippets.

@doyle-flutter
Last active February 17, 2021 09:06
Show Gist options
  • Save doyle-flutter/6eaaa41d1d3c1e7645b0bba56dcfea56 to your computer and use it in GitHub Desktop.
Save doyle-flutter/6eaaa41d1d3c1e7645b0bba56dcfea56 to your computer and use it in GitHub Desktop.
유튜브 및 네이버 카페 이강원님 질문(https://cafe.naver.com/flutterjames)
// 일부 내용입니다
app.use(express.json());
app.get('/req', (req,res) =>{
res.send(`
<!DOCTYPE html>
<html>
<head>
<meta name='viewport'
content='user-scalable=no, initial-scale=1, maximum-scale=1,
minimum-scale=1, width=device-width, height=device-height' />
<meta charset='utf-8' />
<title>제임쓰 Dev : QnA</title>
</head>
<body>
<h2>안녕하세요 이강원님 😈</h2>
<div>
<p>res.sendFile을 통해 HTML 파일로 사용하시는게 편리합니다.</p>
<button onClick="myApp()">버튼</button>
</div>
<script>
function myApp(){
james.postMessage(true);
// (2) 웹뷰 클라이언트에서 다시 서버로 요청 - 결국 플러터 화면
fetch("http://127.0.0.1:3003/req2",{
method: "POST",
body: JSON.stringify({"id":"Flutter WebView 에서 보낸 ID : USER_ID2"}),
headers: {
'Content-Type': 'application/json;charset=UTF-8',
},
})
.then(e => e.json())
.then(v => console.log(v))
.catch(e => console.log(e));
return;
}
</script>
</body>
</html>
`);
});
app.post('/req2',(req,res) => {
var id = req.body?.id ?? "없음";
console.log(id); // 있으면 id, 없으면 "없음"
console.log("데이터를 저장 또는 다르게 활용할 Router 입니다");
// DB 저장을 위해, 작성해주신 DB 쿼리를 이용하였습니다
// DB를 설정하지 않았으므로 try-catch를 걸어두었습니다
try {
var sql = "INSERT INTO actapp (id) VALUES (?)";
connection.query(sql,[id], function (err, results, fields) {
if (err) {
console.log(error);
return res.json(false);
}
// 저장까지 성공
console.log(results);
return res.json(true);
});
} catch (error) {
return res.json(false);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment