0

I need to create a NodeJS application which serves only for exposing REST APIs. When I created a ExpressJS project using Express generator express myNodeApp, it creates a project defaulting the view to .jade files.

Can I create and run a NodeJS project without views ? My NodeJS project will expose REST services which another client application will consume. Hence my NodeJS project do not need any UI elements. Also what package.json or .bin/www file will have. I will be hosting my NodeJS project in Azure cloud and my client application will consume the exposed service from cloud.

Joseph
  • 1,060
  • 3
  • 22
  • 53

3 Answers3

1

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.

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.

rsp
  • 107,747
  • 29
  • 201
  • 177
  • Woop Woop :) well explained sir – Greg Rebisz Jul 31 '17 at 09:54
  • I tried but copying this module in my app.js, and when doing "npm start", it invokes bin/www and throws consecutive errors. Initially port setting error. On resolving that it throws listener error. I used Express generator app – Joseph Aug 01 '17 at 14:15
0

You can do this with express - see below

Install express and body-parser, feel free to use the module below

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');
        });
    }
}
Nikolaj Dam Larsen
  • 5,455
  • 4
  • 32
  • 45
Greg Rebisz
  • 156
  • 7
  • I tried but copying this module in my app.js, and when doing "npm start", it invokes bin/www and throws consecutive errors. Initially port setting error. On resolving that it throws listener error. I used Express generator app. – Joseph Aug 01 '17 at 12:21
0

Yes you can. express is capable to return response other that html element.

However, I would recommend you to use swagger project in developing REST API via express. 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).

This site has a good explanation on how to install, use and run the swagger in NodeJs.

Popoi Menenet
  • 1,018
  • 9
  • 20
  • It is a good option but I do not want to go with .yaml files for my development. can I do with ExpressJS to build a NodeJS APi Application ? – Joseph Aug 01 '17 at 12:22