简体   繁体   English

如何在没有视图的情况下创建 NodeJS 应用程序? 只有 NodeJS REST API

[英]How to Create NodeJS Application without view ? Only NodeJS REST API

I need to create a NodeJS application which serves only for exposing REST APIs.我需要创建一个仅用于公开 REST API 的 NodeJS 应用程序。 When I created a ExpressJS project using Express generator express myNodeApp , it creates a project defaulting the view to .jade files.当我使用 Express generator express myNodeApp创建一个 ExpressJS 项目时,它会创建一个默认视图为 .jade 文件的项目。

Can I create and run a NodeJS project without views ?我可以在没有视图的情况下创建和运行 NodeJS 项目吗? My NodeJS project will expose REST services which another client application will consume.我的 NodeJS 项目将公开另一个客户端应用程序将使用的 REST 服务。 Hence my NodeJS project do not need any UI elements.因此我的 NodeJS 项目不需要任何 UI 元素。 Also what package.json or .bin/www file will have.还有什么package.json.bin/www文件将有。 I will be hosting my NodeJS project in Azure cloud and my client application will consume the exposed service from cloud.我将在 Azure 云中托管我的 NodeJS 项目,我的客户端应用程序将使用来自云的公开服务。

For an example see the code in this answer:有关示例,请参阅此答案中的代码:

Stripping all unnecessary code it would be basically:剥离所有不必要的代码基本上是:

const express = require('express');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.json());

app.post('/email', (req, res) => {
  console.log(req.body.address);
  res.json({ ok: true });
});

app.listen(4443, () => console.log('Listening on http://localhost:4443/'));

This code is a very simple REST API that exposes one endpoint but you can easily add more.此代码是一个非常简单的 REST API,它公开一个端点,但您可以轻松添加更多端点。

But if you're building a RESTful API from scratch then you can consider using some other frameworks like: Hapi, Restify, LoopBack, and other frameworks listed on http://nodeframework.com/ - Express is a very solid but fairly minimal framework and it's not the only option out there.但是,如果您从头开始构建 RESTful API,那么您可以考虑使用其他一些框架,例如:Hapi、Restify、LoopBack 和http://nodeframework.com/ 上列出的其他框架 - Express 是一个非常可靠但相当小的框架这不是唯一的选择。

You can do this with express - see below你可以用 express 来做到这一点 - 见下文

Install express and body-parser, feel free to use the module below安装 express 和 body-parser,可以随意使用下面的模块

var express = require('express');
var app = express();
var bodyParser = require('body-parser');

module.exports = {
    init: function(module_Enabled){
        var portnum = 1234;    process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

        var allowCrossDomain = function(req,÷ res, next) {
            res.header('Access-Control-Allow-Origin', '*');
            res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
            res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');

            // intercept OPTIONS method
            if ('OPTIONS' == req.method) {
                res.send(200);
            } else {
                next();
            }
        };
        app.use(bodyParser.urlencoded({
            extended: false
        }));
        app.use(bodyParser.json());
        app.use(allowCrossDomain);
        var server = app.listen(portnum, function() {
            var host = server.address().address;
            var port = server.address().port;
            console.log("Content Provider Service listening at http://%s:%s", host, port);
        });
        app.get('/', function(req, res) {
            res.send('data');
        });
    }
}

Yes you can.是的你可以。 express is capable to return response other that html element. express能够返回该 html 元素以外的响应。

However, I would recommend you to use swagger project in developing REST API via express .但是,我建议您在通过express开发 REST API 时使用swagger 项目 The project will surely come in handy when developing and MAINTAINING API, especially if your API is huge and complex (lots of url and operation).该项目在开发和维护 API 时肯定会派上用场,尤其是当您的 API 庞大而复杂(大量 url 和操作)时。

This site has a good explanation on how to install, use and run the swagger in NodeJs.这个站点对如何在 NodeJs 中安装、使用和运行 swagger 有很好的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM