SpringCloud(026篇)简单异构系统之nodejs微服务——一、总体介绍1、由于以后要用SpringCloud来集成异构系统,所以本章有nodejs微服务;2.本章使用最简单的http请求拦截url,拦截不同的url后缀做不同的响应处理,就这么简单;2.实现步骤2.1添加nodejs服务端js文件(springms-node-servicenode-service.js)//nodejs介绍http,url,path模块varhttp=require('http');varurl=require("url");varpath=require('path');//创建服务器varserver=http.createServer(function(req,res){//获取请求的路径varpathname=url.parse(req.url).pathname;res.writeHead(200,{'Content-Type':'application/json;charset=utf-8'});//访问http://localhost:8205/,返回{"index":"Welcometothe简单异构系统nodejs服务首页"}if(pathname==='/'){res.end(JSON.stringify({"index":"欢迎来到简单异构系统nodejs服务首页"}));}//访问http://localhost:8205/health,返回{"status":"UP"}elseif(pathname==='/health.json'){res.end(JSON.stringify({"status":"UP"}));}//否则返回404else{res.end("404");}});//创建监听器并打印日志server.listen(8205,function(){console.log('开始监听本地端口:8205');});2.2如何启动windows窗口执行命令:node.exenode-service.js3.Test/************************************************************************************************一、简单异构系统的nodejs微服务:1、编写node-service.js文件;2.启动服务(windows命令);3.输入节点。exenode-service.js命令,正常情况下,会打印“Startlisteningtolocalport:8205”,表示启动成功;注:关于node.exe命令要下载的安装包请移步找度娘,相信大家的聪明才智很快就能搞定这个命令的最简单用法;4.新建一个页面标签,输入http://localhost:8205/,然后打印信息为:{"index":"WelcometoSimpleheterogeneoussystemnodejsservicehomepage"}5.新建一个网页标签,输入http://localhost:8205/health.json,然后打印信息为:{"status":"UP"}5.新建一个网页Tab,输入http://localhost:8205/abc,打印出来信息是:404总结:简单的nodejs微服务处理客户端请求就这么简单,所以市面上有很多服务器都是用nodejs来玩的;******************************************************************************************************/四、下载地址https://gitee.com/ylimhhmily/SpringCloudTutorial.gitSpringCloudTutorial交流QQ群:235322432SpringCloudTutorial交流微信群:微信交流群二维码图片链接欢迎关注,您的支持绝对是我最大的支持!!!
