I have installed this ng2-file-upload package but i am having some problems while upload a image its showing me the following error in console
OPTIONS http://localhost:3000/api/ 500 (Internal Server Error)
XMLHttpRequest cannot load http://localhost:3000/api/. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 500.
my api server.js file code is
var express = require('express');
var multer = require('multer');
var fs = require('fs');
var app = express();
var DIR = './uploads/';
var upload = multer({dest: DIR});
app.use(function (req, res, next) {
res.setHeader("Access-Control-Allow-Methods",
"POST, PUT, OPTIONS, DELETE, GET");
header('Access-Control-Allow-Origin: http://localhost:4200');
header('Access-Control-Allow-Methods: GET, POST, PATCH,
PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type');
next();
});
app.get('/api', function (req, res) {
res.end('file catcher example');
});
app.post('/api', function (req, res) {
upload(req, res, function (err) {
if (err) {
return res.end(err.toString());
}
res.end('File is uploaded');
});
});
var PORT = process.env.PORT || 3000;
app.listen(PORT, function () {
console.log('Working on port ' + PORT);
});
AdminComponent.ts code
import { FileSelectDirective, FileDropDirective, FileUploader,
Headers } from 'ng2-file-upload/ng2-file-upload';
const URL = 'http://localhost:3000/api/';
@Component({
selector : 'app-admin',
templateUrl : './admin.component.html',
styleUrls : ['./admin.component.css']
})