【问题标题】:Unable to hit my local API route in Express无法在 Express 中访问我的本地 API 路由
【发布时间】:2015-05-12 14:34:28
【问题描述】:

我有一个 serverclient 文件夹,在我的客户端文件夹中我有 2 个 Angular 应用程序 /website/dashboard

我当前的路由设置(仅用于开发)去/ 将加载网站应用程序和视图,/dashboard 加载仪表板:

//website api ==================================================================
var website = express.Router();
app.use('/', website);
app.use('/', express.static("../client/"));
console.log(__dirname + "../client/");

website.use(function(req, res, next) {
    console.log(req.method, req.url);

    next();
});

website.get('/', function(req, res) {
    var path = 'index.html';
    res.sendfile(path, { 'root': '../client/website/' });
});

//dashboard api ================================================================
var dashboard = express.Router();
app.use('/dashboard', dashboard);

dashboard.use(function(req, res, next) {
    console.log(req.method, req.url);

    next();
});

dashboard.get('/dashboard', function(req, res) {
    var path = 'index.html';
    res.sendfile(path, { 'root': '../client/dashboard/' });
});

// API to add new accounts:
app.post('/api/accounts/', accountsController.create);

在我的仪表板应用程序中,我的帐户控制器有以下 $resource 调用:

var Account = $resource('/api/accounts');

假设在我的服务器 main.js 上命中此 API:

// API to add new accounts:
app.post('/api/accounts/', accountsController.create);

但目前在通话中遇到 404

'POST http://localhost:9999/api/accounts 404(未找到)'

【问题讨论】:

  • 也许是斜线不匹配的问题?试试app.post('/api/accounts', accountsController.create);
  • @PeterLyons 谢谢!你想继续发布答案吗?不知道尾随/匹配在这里会那么严格

标签: angularjs node.js api express ngresource


【解决方案1】:

我认为这是您的 angularjs 代码和您的 expressjs 代码之间的斜杠与没有斜杠的不匹配。试试app.post('/api/accounts', accountsController.create);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-01
    • 2018-09-25
    • 1970-01-01
    • 2019-04-03
    • 2019-09-13
    • 2016-06-22
    • 2021-03-11
    • 1970-01-01
    相关资源
    最近更新 更多