- Why use Nodejs to render?
Before the front-end task is to use the html+css to the High-fidelity restore UI design of the original image, occasionally using a small number of JQ to add some effects to the page, the page will be paid to the backend developers for data filling (php jsp) and other template language, front-end dependence is serious, Almost any problem is only the front and back of the coordination to solve, and then the front and back end of the separation, that is, the front and back end only with JSON to communicate, the frontend by using Ajax to fill the data, but the use of Ajax is not conducive to SEO optimization and first screen rendering, will cause additional traffic overhead (mainly on the mobile side).
My idea is that Nodejs is only responsible for rendering the page, the data and background logic is implemented by the Java background, and Nodejs uses the data interface provided by Java to render the Page.
The following is a slightly uglier part of the code:
var express = require (' Express '); var router = express. Router (); var http = require (' http ');//var getdata = require ('.. /http ');/* GET Home Page. */
Home router.get ('/', function (req, res, Next) {
Create request var xml = http.request ({host: "localhost", port:3000, method: "GET", path: "/data/", agent:false, header:{' content-type ': ' Application/json '}},function (resouce) {resouce.setencoding (' utf-8 '); Resouce.on (' data ', function (progress) {///here is the processing of the accepted data Res.render (' index ', json.parse (progress)); }); Resouce.on (' End ', function (data) {}); Xml.on (' error ', function (err) {console.log ('-----error------', err); }) }); Xml.end (); Tell the server to end this request});
Analog Java Interface Router.get ('/data ', function (req, res, next) {res.send ({"id": "ticket", "status": "flow order status", "data": "approval Date") , "business_data": "business date", "company": "entrusted unit", "bussiness_company": "business branch", "bus_person": "salesman", "provide_perso N ":" Tariff provider "," Handle_person ":" Operation Branch "," handle_cus ":" operator customer Service "," search ":" query "," num ":" serial number "," bus_id ":" business turnover order ", "work_num": "work number", "odd_id": "flow order type", "pass_date": "approval by date", "company_client": "delegate unit", "bus_class": "business type", "ha Ulier ":" Carrier "});}); Module.exports = router;
App.js
varExpress = Require (' Express ');varPath = require (' path ');varFavicon = require (' Serve-favicon '));varLogger = require (' Morgan ');varCookieparser = require (' cookie-parser '));varBodyparser = require (' body-parser '));varindex = require ('./routes/index '));varUsers = Require ('./routes/users '));varExphbs = require (' express-handlebars '));varApp =Express ();//View engine SetupApp.set (' views ', path.join (__dirname), ' views '));
Set up the template engine App.engine (' HBS ', Exphbs ({layoutsdir:' Views ', Defaultlayout:' Layout ', Extname:'. HBS '}); App.set (' View engine ', ' HBS ');//uncomment after placing your favicon in/public//app.use (favicon (path.join (__dirname, ' public ', ' Favicon.ico ' ));App.use (logger (' Dev ')) ; app.use (bodyparser.json ()); app.use (bodyparser.urlencoded ({extended:false}); app.use (cookieparser ()); app.use (express.static (' Public ')); App.use (‘/‘, index); app.use ('/users ', users);//catch 404 and forward to error handlerApp.use (function(req, res, Next) {varErr =NewError (' not Found '); Err.status= 404; Next (err);});//Error HandlerApp.use (function(err, req, res, Next) {//set locals, only providing error in developmentRes.locals.message =err.message; Res.locals.error= Req.app.get (' env ') = = = ' Development '?err: {}; //render the error pageRes.status (err.status | | 500); Res.render (' Error ');}); Module.exports= app;
Nodejs Rendering Templates