我刚尝试使用Express js创建一个测试API应用程序。我在互联网上采用了许多方法来部署和运行node js api app。但在我的情况下,没有任何工作。请帮帮我。
其他方面我将不得不离开azure或节点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);
{
"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 :(得分: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);
答案 1 :(得分:0)
如果您将其更改为:
,这将有效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);
该应用现在可以处理以下请求:
GET /things
POST /things
GET /hello
POST /hello