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
| const fs = require('fs'); const path = require('path'); const url = require('url'); const ejs = require('ejs');
let getMime = (extName) => { let data = fs.readFileSync('./data/mime.json'); let mimeObj = JSON.parse(data.toString()); return mimeObj[extName]; }
let app = { static(req, res, staticPath) { let pathname = url.parse(req.url).pathname; let extName = path.extname(pathname); pathname = pathname == '/' ? '/index.html' : pathname;
if (pathname != '/favicon.ico') { try { let data = fs.readFileSync('./' + staticPath + pathname); if (data) { let mimeName = getMime(extName); res.writeHead(200, { 'Content-Type': '' + mimeName + ';charset=utf-8' }); res.end(data); } } catch (error) { } } }, login(req,res){ res.end("login"); }, news(req,res){ res.end("news"); }, form(req,res){ ejs.renderFile('./views/form.ejs',{},(err,data)=>{ res.writeHead(200,{'Content-Type':'text/html,charset="utf-8"'}); res.end(data); }) }, doLogin(req,res){ let postData = ''; req.on('data',(chunk)=>{ postData += chunk; }) req.on('end',()=>{ console.log(postData); res.end(postData); }) }, error(req,res){ res.end("error"); } } module.exports = app;
|