const Koa = require('koa');
const Router = require('koa-router');
const app = new Koa();
const router = new Router();
const views = require('koa-views');
const json = require('koa-json');
const onerror = require('koa-onerror');
const bodyparser = require('koa-bodyparser');
const path = require('path');
const logger = require('koa-logger'); const request = require('request'); // error handler
onerror(app) // middlewares
app.use(bodyparser())
.use(json())
.use(logger())
.use(require('koa-static')(__dirname + '/public'))
.use(views(path.join(__dirname, '/views'), {
options: { settings: { views: path.join(__dirname, 'views') } },
map: { 'html': 'nunjucks' },
extension: 'html'
}))
.use(router.routes())
.use(router.allowedMethods()) // x-response-time app.use(async function(ctx, next) {
const start = new Date();
await next();
const ms = new Date() - start;
ctx.set('X-Response-Time', `${ms}ms`);
}); // logger app.use(async function(ctx, next) {
const start = new Date();
await next();
const ms = new Date() - start;
console.log(`${ctx.method} ${ctx.url} - ${ms}`);
}); app.on('error', (err, ctx) =>
log.error('server error', err, ctx)
); router.get('/', async(ctx, next) => { var bd = "bd";
// 从服务器获取数据 bd = await getData();
ctx.state = {
title: 'Koa2',
name: bd
}
// 发送到页面
return await ctx.render('index', ctx.state) })
//获取服务器数据
async function getData(){
return new Promise((resolve,reject)=>{
request("https://www.baidu.com",(error,response,body)=>{
if(error){
reject("error")
}else{
resolve("thank you");//body
}
})
})
} router.get('/b', async(ctx, next) => {
ctx.state = {
title: 'Koa2',
name: '小明-list'
}
await ctx.render('b', ctx.state)
}) app.listen(3000);
console.log('listen http://localhost:3000');

nodejs获取服务器数据到页面的更多相关文章

  1. XMLHTTPRequestObject获取服务器数据

    http://www.educity.cn/develop/526316.html 在Web客户端使用xmlhttp对象,可以十分方便的和服务器交换数据,我们可以获取和发送任何类型的数据,甚至二进制数 ...

  2. 用$.getJSON() 和$.post()获取第三方数据做页面 ——惠品折页面(1)

    用$.getJSON() 和$.post()获取第三方数据做页面 首页 index.html 页面 需要jquery  和 template-web  js文件   可以直接在官网下载 中间导航条的固 ...

  3. jsonp获取服务器数据的方式

    jsonp获取服务器的数据,有两种 一,跨域 二,不跨域 如果跨域 js的写法有两种 1, <script type="text/javascript"> $(func ...

  4. 十一、React 获取服务器数据: axios插件、 fetch-jsonp插件的使用

    react获取服务器APi接口的数据: react中没有提供专门的请求数据的模块.但是我们可以使用任何第三方请求数据模块实现请求数据 一.axios 获取Api数据 使用文档:https://www. ...

  5. 用nodejs创建服务器显示html页面

    nodejs版本:v10.14.2 1.首先准备一个简单的html页面 <!DOCTYPE html> <html lang="en"> <head& ...

  6. Android-----使用SoapObject获取服务器数据

    新建两个工具类ConnectWeb.java 和 ConnectMethod.java 进行对服务器进行数据交互 ConnectWeb.java代码如下: public class ConnectWe ...

  7. 【CMDB】获取服务器数据

    一.通过agent的方式 原理:服务器定制执行py文件通过subprocess模块采集数据发送给数据收集的机器 数据收集的机器:192.168.11.62 服务器:192.168.11.169 数据收 ...

  8. 使用nodejs搭建服务器显示HTML页面

    首先安装express 在命令行输入:npm install express -g 安装完成后可以查看安装情况:npm ls -g 然后创建server.js文件 var express = requ ...

  9. IOS开发之Post 方式获取服务器数据

    //1.创建post方式的 参数字符串url +(NSString *)createPostURL:(NSMutableDictionary *)params { NSString *postStri ...

随机推荐

  1. rtp协议详解/rtcp协议详解

    转自:http://www.cnblogs.com/li0803/archive/2010/11/20/1882792.html 1.简介 目前,在IP网络中实现实时语音.视频通信和应用已经成为网络应 ...

  2. 测试xxxxxxxx

    测试sdfs xxxxxxxbgssdfsdf f sd=JS-demp.zip f /**  * @author John Smith <john.smith@example.com>  ...

  3. linux 自启动

    使用chkconfig命令可以查看在不同启动级别下课自动启动的服务(或是程序),命令格式如下:chkconfig --list可能输出如下:openvpn 0:关闭 1:开启 ...... 6:关闭 ...

  4. SpringCloud系列十二:手动创建Feign

    1. 回顾 上文讲解了自定义Feign.但是在某些场景下,前文自定义Feign的方式满足不了需求,此时可使用Feign Builder API手动创建Feign. 本文围绕以下场景,为大家讲解如何手动 ...

  5. window安装rabbitmq

    Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装Rabbit MQ的前提是安装Erlang.通过下面两个连接可以下载安装最新的版本: 下载并安装Eralng OTP For Wi ...

  6. Atitit.编程语言原理---方法重载的实现与设计 调用方法的原理

    Atitit.编程语言原理---方法重载的实现与设计 调用方法的原理 1. 重载包括:普通方法的重载和构造方法的重载 1 1.1. 横向重载”和“纵向重载”1 1.2. 方法签名通过  方法名称,参数 ...

  7. Unix 环境高级编程

    UNIX 环境高级编程 本书描述了UNIX系统的程序设计接口--系统调用接口和标准C库提供的很多函数. 与大多数操作系统一样,Unix为程序员运行提供了大量的服务--打开文件,读文件,启动一个新程序, ...

  8. JVM内存监控:visualVM jconsole jstatd jmap

    本文是亲自测试的详细配置过程,不是转载而且linux下不需修改/etc/hosts文件 由于在建项目的需要,监控tomcat的内存使用,检查内存泄漏的情况.其实JDK自身已经提供了很多工具,都在JAV ...

  9. [转]成员函数指针与高性能的C++委托

    原文(作者:Don Clugston):Member Function Pointers and the Fastest Possible C++ Delegates 译文(作者:周翔): 成员函数指 ...

  10. 内核交互--sysfs

    文档介绍:http://lxr.linux.no/linux+v2.6.37/Documentation/filesystems/sysfs.txt The sysfs Filesystem Sysf ...