テンプレートエンジンの書き換えをしています。 途中のSwitch-case文の2つめの
caseにエラーが出ています。 修正箇所あるなら教えてください。以下にコードを書きます。
node.js
1'use strict'; 2const http = require('http'); 3const pug = require('pug'); 4const server = http.createServer((req, res) => { 5 const now = new Date(); 6 console.info('[' + now + '] Requested by ' + req.connection.remoteAddress); 7 res.writeHead(200, { 8 'Content-Type': 'text/html; charset=utf-8' 9 }); 10 11 switch (req.method) { 12 case 'GET': 13 if (req.url === '/enquetes/yaki-shabu') { 14 res.write(pug.renderFile('./form.pug', { 15 path: req.url, 16 firstItem: '焼き肉', 17 secondItem: 'しゃぶしゃぶ' 18 })); 19 20 res.end(); 21 break; 22 case 'POST': 23 let body = []; 24 req.on('data', (chunk) => { 25 body.push(chunk); 26 }).on('end', () => { 27 body = Buffer.concat(body).toString(); 28 const decoded = decodeURIComponent(body); 29 console.info('[' + now + '] 投稿: ' + decoded); 30 res.write('<!DOCTYPE html><html lang="ja"><head><meta charset="utf-8"></head><body><h1>' + 31 decoded + 'が投稿されました</h1></body></html>'); 32 }); 33 res.end(); 34 break; 35 default: 36 break; 37 } 38 39}).on('error', (e) => { 40 console.error('[' + new Date() + '] Server Error', e); 41}).on('clientError', (e) => { 42 console.error('[' + new Date() + '] Client Error', e); 43}); 44const port = 8000; 45server.listen(port, () => { 46 console.info('[' + new Date() + '] Listening on ' + port); 47}); 48 49
pug
1doctype html 2html(lang="ja") 3 head 4 meta(charset="UTF-8") 5 title アンケート 6 body 7 h1 どちらが食べたいですか? 8 from(method='post' action="path") 9 span 名前: 10 input (type="text" nama=name) 11 input(type="radio" name="favorite" value=firstItem) 12 span #{firstItem} 13 input(type="radio" name="favarite" value=secondItem) 14 span #{secondItem} 15 button(type="submit") 投稿 16
Visual Studio Codeで記述していますが、そこのエラーで
宣言または、ステートメントが必要ですとなっているのです。
Switch-case文のとこにエラーがあるのでしゅうか?
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。