我正在MEAN堆栈上开发一个应用程序,它可以在应用程序上从磁盘上传图像,或者我们可以使用chrome扩展名发送图像。现在我已经完成了从磁盘上传图像,但是当我尝试使用chrome扩展程序发送图像时。在此网址“http://localhost:9000/api/look/upload”; It gives an error 401 unauthorized
我正在附加 Route.js 代码,如果有任何MEAN堆栈开发人员指导我到底哪里出错了?我也会上传文件。提前谢谢
/**
* Main application routes
*/
'use strict';
var errors = require('./components/errors');
var auth = require('./auth/auth.service');
var path = require('path');
module.exports = function(app) {
// Insert routes below
app.use('/api/users', require('./api/user'));
app.use('/auth', require('./auth'));
app.use('/api/look', require('./api/look'));
app.use('/api/links', require('./api/imgScraper'));
app.use('/api/comments', require('./api/comments'));
app.post('/forgotpassword', require('./forgotpassword').reset);
// All undefined asset or api routes should return a 404
app.route('/:url(api|auth|components|app|bower_components|assets)/*')
.get(errors[404]);
app.route('/*')
.get(function(req, res) {
res.sendFile(path.resolve(app.get('appPath') + '/index.html'));
});
};
module.exportstest = function(app) {
app.use('/api/looktest', require('./api/looktest'));
};
JSON CODE Is here:
JSON CODE:
{
"name": "IBR Extension",
"version": "1.3",
"description": "Demonstrate screenshot functionality in the chrome.tabs api.",
"background": {
"persistent": false,
"scripts": ["ExtensionWorking.js"]
},
"browser_action": {
"default_icon": "camera.png",
"default_title": "Take a screen shot!"
},
"permissions": [
"tabs",
"activeTab",
"downloads",
"<all_urls>"
],
"manifest_version": 2
}
XHR code is here:
function reportBug(){
var imageData = document.getElementById("canvas").toDataURL();
var xhttp = new XMLHttpRequest();
var params = "imageData=" + imageData;
var url = "http://localhost:9000/api/look/upload";
xhttp.open( "POST", url, true ); // false for synchronous request
xhttp.send( params );
return xhttp.responseText;
}
Upload function is here:
exports.upload = function(req, res) {
console.log('abc');
var newLook = new Look();
var fileimage = req.middlewareStorage.fileimage;
console.log(req.body + "test test");
newLook.image = '/assets/images/uploads/' + fileimage;
newLook.email = req.body.email;
newLook.linkURL = req.body.linkURL;
newLook.title = req.body.title;
newLook.description = req.body.description;
newLook.userName = req.body.name;
newLook._creator = req.body._creator;
newLook.createTime = Date.now();
newLook.upVotes = 0;
newLook.save(function(err, look) {
if(err) {
console.log('error saving look');
return res.send(500);
} else {
console.log(look);
res.status(200)
.send(look);
}
});
};