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.

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Woop Woop :) well explained sir
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
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.

1 Comment

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 ?
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');
        });
    }
}

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.