kmrrakesh
kmrrakesh

Reputation: 147

express js api end point does not work on azure web app

I just tried creating a test api app using Express js. i embraced many ways available on internet to deploy and run node js api app. But nothing is working in my case. Please help me.

Other wise i will have to leave either azure or node js.

I am attaching all the files below: 1.index.js

var express = require('express');
var app = express();


router.get('/things', function(req, res) {
    res.json('GET route on things.');
});
router.post('/things', function(req, res) {
    res.json('POST route on things.');
});

app.get('/hello', function(req, res) {
    res.json("Hello World!");
});

app.post('/hello', function(req, res) {
    res.json("You just called the post method at '/hello'!\n");
});

const port = process.env.PORT || 1337;
app.listen(port);
2.package-lock.json

{
    "name": "testabc",
    "version": "1.0.0",
    "description": "test api",
    "main": "index.js",
    "engines": {
        "node": "^7.10.0"
    },
    "scripts": {
        "test": "test",
        "start": "node index.js",
        "prestart": "npm install"
    },
    "repository": {
        "type": "git",
        "url": "none"
    },
    "keywords": [
        "test"
    ],
    "author": "rakesh",
    "license": "ISC",
    "dependencies": {
        "express": "^4.16.2"
    }
}

3.package.json

{
    "name": "testabc",
    "version": "1.0.0",
    "description": "test api",
    "main": "index.js",
    "engines": {
        "node": "^7.10.0"
    },
    "scripts": {
        "test": "test",
        "start": "node index.js",
        "prestart": "npm install"
    },
    "repository": {
        "type": "git",
        "url": "none"
    },
    "keywords": [
        "test"
    ],
    "author": "rakesh",
    "license": "ISC",
    "dependencies": {
        "express": "^4.16.2"
    }
}

4.web.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.webServer>
        <handlers>
            <add name="iisnode" path="index.js" verb="*" modules="iisnode" />
        </handlers>   
        <rewrite>
      <rules>
        <rule name="myapp">
          <match url="/*" />
          <action type="Rewrite" url="index.js" />
        </rule>
      </rules>
    </rewrite>         
    </system.webServer>
</configuration>

Upvotes: 2

Views: 1479

Answers (2)

Aaron Chen
Aaron Chen

Reputation: 9950

This would work if you change it to:

var express = require('express');
var app = express();

//****************************************
app.route('/things')
    .get(function (req, res) {
        res.json('GET route on things.');
    })
    .post(function (req, res) {
        res.json('POST route on things.');
    })
//****************************************
// or
//****************************************
var router = express.Router();
router.get('/', function (req, res) {
    res.json('GET route on things.');
});
router.post('/', function (req, res) {
    res.json('POST route on things.');
});
app.use('/things', router);
//****************************************

app.get('/hello', function (req, res) {
    res.json("Hello World!");
});

app.post('/hello', function (req, res) {
    res.json("You just called the post method at '/hello'!\n");
});

const port = process.env.PORT || 1337;
app.listen(port);

The app will now be able to handle requests to:

  • GET /things
  • POST /things
  • GET /hello
  • POST /hello

Reference: https://expressjs.com/en/guide/routing.html

Upvotes: 1

kmrrakesh
kmrrakesh

Reputation: 147

var express = require('express');
var app = express();
var router = express.Router();

router.get('/things', function(req, res) {
    res.json('GET route on things.');
});
router.post('/things', function(req, res) {
    res.json('POST route on things.');
});

app.get('/hello', function(req, res) {
    res.json("Hello World!");
});

app.post('/hello', function(req, res) {
    res.json("You just called the post method at '/hello'!\n");
});

const port = process.env.PORT || 1337;
app.listen(port);

Upvotes: 1

Related Questions