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