2017-12-05 10 views
0

Express jsを使用してテストapiアプリケーションを作成しようとしました。私は、インターネット上で利用可能な多くの方法を採用して、ノードjs APIアプリケーションを展開して実行します。しかし、私の場合は何も働いていません。私を助けてください。エクスプレスjs apiエンドポイントが晴れのあるWebアプリケーションでは機能しない

他の賢明な私は紺やノードjsを残す必要があります。

私は下のすべてのファイルを添付しています: 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>

+0

すべてのコードを提供するのではなく、エラーを確認することをお勧めします。 – Deep

+0

「探しているリソースが削除された、名前が変更された、一時的に使用できない」などのエラーが発生することがあります。 – kmrrakesh

+0

"hello"という経路にしか反応しませんが、 "things"という経路を消費しようとすると、上記のエラーが発生します。 – kmrrakesh

答えて

0

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);

+0

それはindex.jsの変更されたコードです、最初の投稿で私の誤植を許してください、私はちょうど "varルータ= express.Router()を追加しました" – kmrrakesh

0

あなたはそれを変更する場合、これは動作します:

  • GET /things
  • POST /things
  • GET /hello
  • POST /hello
  • :アプリは今までのリクエストを処理することができるようになります

    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); 
    

参考:https://expressjs.com/en/guide/routing.html

関連する問題