如何将nodejs与API一起使用 [英] How do I use nodejs with an API

查看:143
本文介绍了如何将nodejs与API一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个搜索twitter主题标签然后在网页上显示的应用程序。我正在使用NodeJS。我已经按照教程开始工作了。



然而,正如我想要学习的那样,我有一些问题:



如果我理解正确,我需要使用Express来服务我项目的客户端(在网页上显示结果)。这是将NodeJS与API一起使用的正确方法还是有其他方法?



在我的应用程序中,我使用hogan-express作为模板引擎,我是否需要使用模板引擎或者我可以使用普通的html?



感谢您的回答



我的尝试:



这是我的项目

package.json

I'm trying to build an application that search for twitter hashtags and then displays them on a webpage. I'm using NodeJS for this. I've followed a tutorial and got it to work.

However, as I'm trying to learn, I've got some questions:

If I've understand correctly I need to use Express to serve the client side of my project (display the results on a webpage). Is this the right way to use NodeJS with an API or are there other ways?

In my application, I'm using hogan-express as template engine, do I need to use a template engine or can I use just normal html?

Thank you for your answers

What I have tried:

This is my project
package.json

{
  "name": "twitter-express",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.3",
    "hogan-express": "^0.5.2",
    "twit": "^2.2.9"
  }
}





server.js



server.js

var express = require('express');
// create instance of var express
var server = express();

server.set('port', process.env.PORT || 3003);
server.engine('html', require('hogan-express'));
server.set('view engine', 'html');

server.get('/', function(req, res) {
    res.render('index', {title: 'Twitter', data: theTweets});
});

server.listen(server.get('port'), function() {
    console.log('Listening on port:' + server.get('port'));
});

var Twit = require('twit');
var theTweets = [];

var T = new Twit({
    consumer_key: 'dhooGTRTJS9kYnOWT19S6Pofj',
    consumer_secret: 'e7uK3EltqdS0JMuIK7OKdPuNrwpBajuLYq37okJ7plIQeRzPgQ',
    access_token: '492232617-HMmUUkWTfqgGBo2LKF4cIn1rRO7ujyNAUevgGrZ3',
    access_token_secret: 'RQjrqMMwNUkTWfK22uKx7tT5Ckk9ddQW3WNY1eo7FXnzy'
});

const params = {
    q: '#nodejs',
    count: 10,
    result_type: 'recent',
    lang: 'en'
}

T.get('search/tweets', params, getData);

function getData(err, data, response) {
    var tweets = data.statuses;
    for (var i = 0; i < tweets.length; i++) {
        theTweets.push(tweets[i].text);
    }
}

推荐答案

Quote:

如果我理解正确,我需要使用Express来服务我项目的客户端(在网页上显示结果)。这是将NodeJS与API一起使用的正确方法还是有其他方法?

If I've understand correctly I need to use Express to serve the client side of my project (display the results on a webpage). Is this the right way to use NodeJS with an API or are there other ways?

哦,不,不,不,你错了。 Express是Node.js的服务器包装器,为您提供服务器端编程服务和功能。在您自己的代码中,请检查,

Oh, no no no, you are all wrong. Express is a server wrapper for Node.js, and provides you with the server-side programming services and functionality. In your own code, please check,

// Server is the Express and it is merely returning the HTML; not rendering it.
server.get('/', function(req, res) {
    res.render('index', {title: 'Twitter', data: theTweets});
});



代码中的客户端库可能是 hogan-express [ ^ 这将负责生成动态HTML内容并提供它。尽管如此,我们还没有达到客户端。客户端领域的统治者是JavaScript。我没有看到你注入的任何JavaScript库。



但如果你的意思是,提供请求:那么是的,你是对的,Express用于在Node中提供请求。 js,因为它提供了一种管理服务器的灵活方式。 :-)


The client-side library in your code might be hogan-express[^] and that will be responsible for generating dynamic HTML content and providing it. Still, we have not yet reached the client-side. The ruler of the client-side realm is JavaScript. I do not see any JavaScript library that you are injecting there.

But if you meant, to serve the requests: Then yes, you are right, Express is used to serve the requests in Node.js as it provides a flexible way to manage your server. :-)

Quote:

这是将NodeJS与API一起使用的正确方法还是有其他方法?

Is this the right way to use NodeJS with an API or are there other ways?

是的,Express是一个很棒的库,可以满足您的大多数目的。现在明白这一点,你只是调用API来获得一些结果。在Node.js中,API的首选结果是JSON文档,因为Node.js具有本机支持。



Twit库肯定会有自己的文档,您可以访问并理解以进一步继续。



Yes, Express is a great library to serve most of the purposes that you have in mind. Now understand this, that you are only calling the API to get some result. In Node.js the preferred result from an API is the JSON document, since Node.js has native support for that.

The Twit library would definitely have its own documentation that you can reach and understand to further proceed.

Quote:

在我的应用程序中,我使用hogan-express作为模板引擎,我需要使用模板引擎还是只能使用普通的html?

In my application, I'm using hogan-express as template engine, do I need to use a template engine or can I use just normal html?

这部分你也有点不对,抱歉。 hogan-express早已被其经理,开发人员弃用。检查我为hogan-express附加的链接。我可以推荐使用Pug或EJX,还有许多其他模板库,但这些都有点可忍受。 :D



最后,简单的HTML文件不会为您带来任何好处,因为您需要使用模板来显示页面上的推文,否则,静态页面不包含太多信息 - 除非您计划为每个请求创建新的HTML页面。



NPM上的Twit包 [ ^ ]



你可能会我对我为初学者创建的Node.js示例感兴趣,它展示了Node.js,Express,Pug以及您可以在存储库中找到的更多技术的基础知识:GitHub - afzaal-ahmad-zeeshan / nodejs-dockerized:Dockerized Node.js应用程序。 [ ^ ]

You are a bit wrong in this part too, sorry. hogan-express has been long deprecated by its own manager, developer. Check the link I attached for hogan-express. I can recommend using either Pug, or EJX, there are many other template libraries, but these are somewhat bearable. :D

Lastly, simple HTML files will not serve you any benefit, since you will need to use a template nonetheless to show the tweets on the page, otherwise, a static page won't contain much information — unless you plan to create a new HTML page for each request.

Twit package on NPM[^]

You might be interested in my Node.js sample, that I created for beginners to try and learn, it demonstrates basics of Node.js, Express, Pug and a few more technologies that you can find on the repository: GitHub - afzaal-ahmad-zeeshan/nodejs-dockerized: Dockerized Node.js application.[^]


这篇关于如何将nodejs与API一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆