Nodejs + Express + Ejs Server Demo

Source: Internet
Author: User

varHttp=require ("http");varExpress=require ("Express");varFS = require ("FS");varBodyparser = require ('Body-parser');varCommon = require ("./publice/common");varapp=Express ();//TimervarReftimer =NULL;vartem={title:"I'm the middle section .", Info:[{name:"Davi", Time:1497591600000},{name:"Bill", Time:1497591600000},{name:"can", Time:1497591600000}]};//Memory Cache Account ListvarAccountmap = {};//Initialize the list of accountsfunction init () {//load data from file to Accountmap    vardata =Operatfile (); if(data) Accountmap =data;}//App.use (Bodyparser.json ());//For parsing Application/jsonApp.use (bodyparser.urlencoded ({extended:true}));//For parsing application/x-www-form-urlencoded//Creating a serverHttp.createserver (APP). Listen ( the,"0.0.0.0", function () {Console.log ("Server is listening port");});//mount static resource processing middlewareApp.use (Express.Static(__dirname));//mount static resource processing middleware//App.locals.Common = Common;App.use (function (req, res, next) {Res.locals.Common=Common; Next ();});//to set the catalog for a template viewApp.Set(" views","./views");//Set whether the view compilation cache is enabled and enabled will accelerate server execution efficiencyApp.Set("View Cache",true);//What template engine is used to format the template engineApp.Set("View Engine","Ejs");//set up RoutesApp.Get("/", Function (req,res) {varLogdata = Accountmap | |Operatfile (); returnRes.render ("Index", TEM);}); App.Get("/index", Function (req,res) {varLogdata = Accountmap | |Operatfile (); returnRes.render ("Index", TEM);});//Write an interfaceApp.Get('/history', Function (req,res) {varLogdata = Accountmap | |Operatfile (); //Res.json ({state:0, Data: {title:tem.message,names:["Odl", "DKKD", "Ejs", "KKK"]});    varitem ={title:"I'm the middle section .", Info:[{name:"Davi", Time:1497591600000},{name:"Bill", Time:1497591600000},{name:"can", Time:1497591600000},{name:"can", Time:1497592600000}]    }; returnRes.render ("Tradelog", item);});
Data push (mainly for database server data push over) App.post ('/account/info', Function (req, res) {//Data Format//{User: ' xjl30 ', info: {}}//Save Data//save to file and in memory varparam =json.stringify (req.body); Console.log ("<-----------------------split Line-------------------------->"); Console.log ("Here's the data:"+param); //accountmap[] if(param) {accountmap=param; Operatfile (param); //saving data to a file }});//Data Processingfunction Operatfile (fData) {Console.log ("Open File"); Fs.open ("./log/historylog.txt","w+", function (err, FD) {//w+ Opens the file in read-write mode and creates if the file does not exist. if(err) {returnConsole.error (ERR); } if(FData) fs.writefile ('./log/historylog.txt', FData, function (err) {if(err) {returnConsole.error (ERR); } console.log ("Write file Data"); Fs.close (FD); }); ElseGetlogfile (FD); });} function Getlogfile (FD) {varHistorydata =""; Fs.readfile ('./log/historylog.txt', function (err, data) {if(err) {returnConsole.error (ERR); } if(data.tostring () = =""){ returnConsole.error ("file data is empty"); } historydata=data; Console.log ("Read file Data"); Fs.close (FD); }); returnHistorydata;} Init ();

Nodejs + Express + Ejs Server Demo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.