无法在 Express 中访问我的本地 API 路由
Unable to hit my local API route in Express
我有一个 server 和 client 文件夹,在我的客户端文件夹中我有 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');
应该在我的服务器上点击 API main.js:
// API to add new accounts:
app.post('/api/accounts/', accountsController.create);
但是目前正在通话中收到 404
'POST http://localhost:9999/api/accounts 404 (Not Found)'
我认为这是你的 angularjs 代码和你的 expressjs 代码之间尾部斜线与没有尾部斜线的不匹配。尝试 app.post('/api/accounts', accountsController.create);
。
我有一个 server 和 client 文件夹,在我的客户端文件夹中我有 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');
应该在我的服务器上点击 API main.js:
// API to add new accounts:
app.post('/api/accounts/', accountsController.create);
但是目前正在通话中收到 404
'POST http://localhost:9999/api/accounts 404 (Not Found)'
我认为这是你的 angularjs 代码和你的 expressjs 代码之间尾部斜线与没有尾部斜线的不匹配。尝试 app.post('/api/accounts', accountsController.create);
。