I have installed cors & body parser. This is my authSrvice.js
file
resetemail(emailid) {
let headers = new Headers();
headers.append('Content-Type','application/json');
return this.http.post('http://localhost:3000/api/femail',emailid,{headers:headers})
.map(res => res.json());
}
This is from routes file
const User = require('../models/user');
router.post('/femail',(req,res,next) => {
const emailid = req.email.body;
User.getUserByUsername(username, (err, user) => {
if(err) throw err;
if(!user){
return res.json({success: false, msg: 'User not found'});
}
else {
return res.json({success: true, msg: 'success'});
}
});
});
In chrome console it's showing an error as
Failed to load http://localhost:3000/api/femail: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access. The response had HTTP status code 400. Cross-Origin Read Blocking (CORB) blocked cross-origin response http://localhost:3000/api/femail with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details.
In my cmd is showing as
SyntaxError: Unexpected token m in JSON at position 0 at JSON.parse () at createStrictSyntaxError (E:\project-1\node_modules\body-parser\lib\types\json.js:158:10) at parse (E:\project-1\node_modules\body-parser\lib\types\json.js:83:15) at E:\project-1\node_modules\body-parser\lib\read.js:121:18 at invokeCallback (E:\project-1\node_modules\raw-body\index.js:224:16) at done (E:\project-1\node_modules\raw-body\index.js:213:7) at IncomingMessage.onEnd (E:\project-1\node_modules\raw-body\index.js:273:7) at emitNone (events.js:106:13) at IncomingMessage.emit (events.js:208:7) at endReadableNT (_stream_readable.js:1064:12) at _combinedTickCallback (internal/process/next_tick.js:138:11) at process._tickCallback (internal/process/next_tick.js:180:9)
This is my server.js
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const cors = require('cors');
const passport = require('passport');
const mongoose = require('mongoose');
const config = require('./config/database');
var methodOverride = require('method-override');
var session = require('express-session')
var nodemailer = require('nodemailer');
var async = require('async');
var crypto = require('crypto');
var LocalStrategy = require('passport-local').Strategy;
var cookieParser = require('cookie-parser');
// Connect To Database
mongoose.connect(config.database);
// On Connection
mongoose.connection.on('connected', () => {
console.log('Connected to database '+config.database);
});
// On Error
mongoose.connection.on('error', (err) => {
console.log('Database error: '+err);
});
const app = express();
const port = 3000;
//app.use(require('connect').bodyParser());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const users = require('./routes/users');
app.use('/api',users);
// CORS Middleware
app.use(cors());
// Set Static Folder
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
res.send('Invalid endpoint');
});
// Body Parser Middleware
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
next();
});
app.use(session({ secret: 'this is a cat' }));
app.use(cookieParser());
// Passport Middleware
app.use(passport.initialize());
app.use(passport.session());
require('./config/passport')(passport);
app.use('/users', users);
app.listen(port, () => {
console.log("Server started on port:", port);
});
I don't know what's wrong.