admin管理员组

文章数量:1387287

Having some trouble setting up the restful API for my express app.

Here is my app.js:

var express = require('express'), 
app = express(),
bodyParser = require('body-parser'), 
methodOverride = require('method-override');
routes = require('./routes'),
api = require('./routes/api'),
port = process.env.PORT || 3000;

app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
app.use(methodOverride());
app.use(bodyParser.urlencoded({extended: true}));

// Page Routes
app.get('/', routes.index);
app.get('/partials/:filename', routes.partials);

// // API Routes
app.get('/api/name', api.name);

app.listen(port, function() {
  console.log('Listening on port ' + port);
});

In /routes/api.js I have the following test function:

exports.name = function (req, res) {
    res.json({
       name: 'Test'
    });
};

Currently I get the following error when i go to http://my_ip/api/name Cannot GET /api/name

Any ideas?

Thanks

Having some trouble setting up the restful API for my express app.

Here is my app.js:

var express = require('express'), 
app = express(),
bodyParser = require('body-parser'), 
methodOverride = require('method-override');
routes = require('./routes'),
api = require('./routes/api'),
port = process.env.PORT || 3000;

app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
app.use(methodOverride());
app.use(bodyParser.urlencoded({extended: true}));

// Page Routes
app.get('/', routes.index);
app.get('/partials/:filename', routes.partials);

// // API Routes
app.get('/api/name', api.name);

app.listen(port, function() {
  console.log('Listening on port ' + port);
});

In /routes/api.js I have the following test function:

exports.name = function (req, res) {
    res.json({
       name: 'Test'
    });
};

Currently I get the following error when i go to http://my_ip/api/name Cannot GET /api/name

Any ideas?

Thanks

Share Improve this question asked Apr 26, 2016 at 3:41 UltraViolentUltraViolent 991 gold badge2 silver badges9 bronze badges 13
  • 1 what you get if you console.log(api.name) app.js – uzaif Commented Apr 26, 2016 at 3:48
  • @uzaif I get: [Function]. The function name() seems to be working fine. Something to do with the path to it – UltraViolent Commented Apr 26, 2016 at 3:50
  • api(app); try this in app.js and check is it work or not? – uzaif Commented Apr 26, 2016 at 3:51
  • Do any of your routes work? – jfriend00 Commented Apr 26, 2016 at 3:52
  • @jfriend00 yeah, i can navigate around the site so the /partials routes seem to be working – UltraViolent Commented Apr 26, 2016 at 3:53
 |  Show 8 more ments

1 Answer 1

Reset to default 2

The following code is working for me. I think there is some issue with your routes package. Can you share the code of 'routes' package and file structure ?

app.js

var express = require('express'), 
app = express(),
routes = require('./routes');
api = require('./routes/api'),
port = process.env.PORT || 3000;

// Page Routes
app.get('/', routes.index);

// API Routes
app.get('/api/name', api.name);

app.listen(port, function() {
  console.log('Listening on port ' + port);
});

/routes/api.js

exports.name = function (req, res) {
   res.json({
     name: 'Test'
   });
};

/routes.js

exports.index = function (req, res) {
   res.json({
     name: 'Index'
   });
};

本文标签: javascriptExpress routing error Cannot Get apinameStack Overflow