<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                router.js ~~~ //該模塊負責封裝所有路由判斷代碼 var path = require('path'); var handler=require('./handler.js'); module.exports = function(req, res) { //根據用戶請求的路徑,將對應的html頁面顯示出來 if (req.url === '/' || req.url === '/index' && req.method === 'get') { handler.index(req,res); } else if (req.pathname === '/item' && req.method === 'get') { handler.item(req,res); } else if (req.url === '/submit' && req.method === 'get') { handler.submit(req,res); } else if (req.url.startsWith('/add') && req.method === 'get') { handler.addGet(req,res); } else if (req.url === '/add' && req.method === 'post') { handler.addPost(req,res); } else if (req.url.startsWith('/resources') && req.method === 'get') { handler.static(req,res); } else { handler.handleErrors(req,res); } } ~~~ handler.js ~~~ //該模塊負責對具體的業務進行處理 var fs = require('fs'); var path = require('path'); var querystring = require('querystring'); //處理請求 / 和 /index 的業務方法 module.exports.index = function(req, res) { readNewsData(function(arr) { //2.在服務器端使用模板引擎,將arr中的數據和index.html文件中的內容結合,渲染給客戶端 res.render(path.join(__dirname, 'views', 'index.html'), { arr: arr }); }); } //處理請求 /item 的方法(顯示新聞詳情) module.exports.item = function(req, res) { //讀取detail.html并返回 //1.獲取當前用戶請求的新聞的id //urlObj.query.id //2.讀取 data.json文件中的數據,根據 id 找到對應新聞 readNewsData(function(list_news) { var model = null; //循環 list_news中的數據,找到和id值相等的數據 for (var i = 0; i < list_news.length; i++) { //判斷集合中是否有與用戶提交的id相等的新聞 if (list_news[i].id.toString() === req.query.id) { //如果找到了對等的新聞 則將其記錄下來 model = list_news[i]; break; } } if (model) { res.render(path.join(__dirname, 'views', 'detail.html'), { item: model }); } else { res.end("NO such item"); } }); } //處理請求 /submit 的方法 module.exports.submit = function(req, res) { //讀取submit.html并返回 res.render(path.join(__dirname, 'views', 'submit.html')); } //處理get方式添加新聞 module.exports.addGet = function(req, res) { readNewsData(function(arr) { req.query.id = arr.length; arr.push(req.query); writeNewsData(JSON.stringify(arr), function() { res.writeHead(302, 'Found', { 'Location': '/' }); res.end(); }); }); } //處理post方式添加新聞 module.exports.addPost = function(req, res) { //1.讀取data.json文件中的數據 readNewsData(function(arr) { //2.讀取用戶post提交的數據 postData(req, function(postBody) { //3.為用戶提交的新聞增加一個id屬性,并把新聞對象push到arr數組中 postBody.id = arr.length; arr.push(postBody); //4.將新的list寫入到data.json文件中 writeNewsData(JSON.stringify(arr), function() { res.writeHead(302, 'Found', { 'Location': '/' }) res.end(); }); }) }); } //處理靜態資源請求 module.exports.static = function(req, res) { //如果用戶請求是以/resources開頭,并且是get請求,就認為用戶是要請求靜態資源 res.render(path.join(__dirname, req.url)); } //處理404錯誤請求 module.exports.handleErrors = function(req, res) { // res.writeHead(404, 'Not Found', { // 'Content-Type': 'text/html;charset=utf-8' // }); // res.end("404,page not found."); res.render(path.join(__dirname, 'views', '404.html')); } //封裝讀取data.json文件的方法 function readNewsData(callback) { fs.readFile(path.join(__dirname, "data", 'data.json'), 'utf8', function(err, data) { if (err && err.code !== 'ENOENT') { throw err; } var arr = JSON.parse(data || '[]'); //通過調用回調函數callback()將讀取到的數據arr 傳遞出去 callback(arr); }); } //封裝寫入data.json文件的方法 function writeNewsData(data, callback) { fs.writeFile(path.join(__dirname, 'data', 'data.json'), data, function(err) { if (err) { throw err; } //調用 callback()來執行當寫入數據完畢后的操作 callback(); }) } //封裝一個獲取用戶 post 提交的數據的方法 function postData(req, callback) { var list = []; req.on('data', function(chunk) { list.push(chunk); }); req.on('end', function() { var postBody = Buffer.concat(list); postBody = postBody.toString('utf8'); postBody = querystring.parse(postBody); //把用戶post提交的數據傳遞出去 callback(postBody); }); } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看