I have used nga admin using node js I'm new to angular js and node js. I have created rest api on localhost using mysql node js and i have check it by using postman and its working fine. But when I parse this api url it's gives me this below error. See I want to create admin panel for users to edit delete and update just need to implement basic thing.But its' showing me 404 error HEADERS ERROR.
XMLHttpRequest cannot load http://localhost:3000/api/user?_page=1&_perPage=30&_sortDir=DESC&_sortField=id. 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' is therefore not allowed access. Server.js file contain this below code.
server.js file
var express = require('express'),
path = require('path'),
bodyParser = require('body-parser'),
app = express(),
expressValidator = require('express-validator');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
app.use(bodyParser.json());
var connection = require('express-myconnection'),
mysql = require('mysql');
app.use(
connection(mysql,{
host : 'localhost',
user : 'root',
password : '',
database : 'gkv',
debug : true //set true if you wanna see debug logger
},'request')
);
app.get('/',function(req,res){
res.send('Welcome');
});
var router = express.Router();
router.use(function(req, res, next) {
console.log(req.method, req.url);
console.log(res.headers);
next();
});
var curut = router.route('/user');
curut.get(function(req,res,next){
res.header("Access-Control-Allow-Headers","Content-Type");
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST,OPTIONS");
req.getConnection(function(err,conn){
if (err) return next("Cannot Connect");
var query = conn.query('SELECT * FROM users',function(err,rows){
if(err){
console.log(err);
return next("Mysql error, check your query");
}
res.json({message: 'All users!',data: rows });
//res.render('user',{title:"RESTful Crud Example",data:rows});
});
});
});
app.use('/api', router);
var server = app.listen(3000,function(){
console.log("Listening to port %s",server.address().port);
});
admin.js file
var myApp = angular.module('myApp', ['ng-admin']);
myApp.config(function($httpProvider) {
//Enable cross domain calls
$httpProvider.defaults.useXDomain = true;
//Remove the header containing XMLHttpRequest used to identify ajax call
//that would prevent CORS from working
delete $httpProvider.defaults.headers.common['X-Requested-With'];
$httpProvider.defaults.headers.common["Access-Control-Allow-Origin"] = "*";
$httpProvider.defaults.headers.common["Accept"] = "application/json";
$httpProvider.defaults.headers.common["content-type"] = "application/json";
});
myApp.config(['NgAdminConfigurationProvider', function (nga) {
// create an admin application
var admin = nga.application('My First Admin')
.baseApiUrl('http://localhost:3000/api/'); // main API endpoint
// create a user entity
// the API endpoint for this entity will be 'http://jsonplaceholder.typicode.com/users/:id
var user = nga.entity('user');
// set the fields of the user entity list view
user.listView().fields([
nga.field('Nachname').isDetailLink(true),
nga.field('Anrede'),
nga.field('Emailadresse'),
nga.field('Telefonummer'),
nga.field('Date&Time'),
nga.field('URL'),
nga.field('UserIP'),
]);
user.creationView().fields([
nga.field('Nachname'),
nga.field('Anrede'),
nga.field('Emailadresse'),
nga.field('Telefonummer'),
nga.field('Date'),
nga.field('URL'),
nga.field('UserIP')
]);
//user.editionView().fields(user.creationView().fields());
admin.addEntity(user);
/*var post=nga.entity('posts');
post.listView().fields([
nga.field('id'),
nga.field('title'),
nga.field('userId','reference').targetEntity(user).targetField(nga.field('username')).label('User'),
nga.field('comments','referenced_list').targetEntity(nga.entity('comments')).targetReferenceField('postId').targetFields([nga.field('email'),nga.field('name')]).sortField('id').sortDir('DESC')
]);*/
// add the user entity to the admin application
//admin.addEntity(post);
// attach the admin application to the DOM and execute it
nga.configure(admin);
}]);