Я новичок в Nodejs. Создал API на локальном сервере, используя Mysql. работа с Angular в качестве интерфейса и использование Nodejs и Express в качестве бэкэнда. Но мое приложение Angular не может подключиться к Node API. в Angular ошибка произошла как:
POST http://localhost:3000/Auth/signUp net::ERR_CONNECTION_REFUSED.
, а на консоли узла ошибка возникла как:
Error
D:\Angular\EMSexpress\node_modules\mysql\lib\protocol\Parser.js:437
throw err; // Rethrow non-MySQL errors
ReferenceError: error is not defined[enter image description here][1]
at Query.<anonymous> (D:\Angular\EMSexpress\Models\model.js:16:25)
at Query.<anonymous> (D:\Angular\EMSexpress\node_modules\mysql\lib\Connection.js:526:10)
at Query._callback (D:\Angular\EMSexpress\node_modules\mysql\lib\Connection.js:488:16)
at Query.Sequence.end (D:\Angular\EMSexpress\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24)
at Query.ErrorPacket (D:\Angular\EMSexpress\node_modules\mysql\lib\protocol\sequences\Query.js:92:8)
at Protocol._parsePacket (D:\Angular\EMSexpress\node_modules\mysql\lib\protocol\Protocol.js:291:23)
at Parser._parsePacket (D:\Angular\EMSexpress\node_modules\mysql\lib\protocol\Parser.js:433:10)
at Parser.write (D:\Angular\EMSexpress\node_modules\mysql\lib\protocol\Parser.js:43:10)
at Protocol.write (D:\Angular\EMSexpress\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (D:\Angular\EMSexpress\node_modules\mysql\lib\Connection.js:88:28)
[nodemon] app crashed - waiting for file changes before starting...
модель. js Файл
// import db from "../db.js";
var db = require("../db")
let model = {
signup: (input, cb) => {
let today = new Date();
let data = {
organisationName: input.organisationName,
email: input.email,
mobileNumber: input.mobileNumber,
password: input.password,
is_Active: 1,
createdAt: today
}
return db.query('INSERT INTO users SET ?' ,[data],(err,result)=>{
console.log("Error");
console.log(error);
console.log("result");
console.log(result);
})`enter code here`
},
findOne: (organisationName,cb) => {
return db.query("SELECT * FROM users WHERE organisationName=? AND is_Active=1",
[organisationName],cb);
},
findMobile: (mobileNumber,cb) => {
return db.query("SELECT * FROM users WHERE mobileNumber=? AND is_Active=1",
[mobileNumber],cb);
}
}
module.exports = model;`
дб. js Файл
[const mysql = require("mysql")
const connection = mysql.createConnection({
host:"localhost",
port:3306,
user: 'root',
password: "123456",
database: "EMS"
})
connection.connect(function(err){
if(err) throw err;
console.log("mysql connected")
})
module.exports = connection;][1]`
Идентификатор файла маршрутизатора js
var express = require("express")
var router = express.Router()`enter code here`
var user = require("../Models/model")
var bcrypt = require("bcrypt")
router.get('/signUp', function(req, res, next) {
res.json({Name: 'Naveen'});
});
router.post('/signUp', (req, res) => {
user.signup(req.body, (error, result) => {
res.json({data: result, error: error })
})
})
module.exports = router;
Angular Сервисный код
url = `${environment.apiUrl}/Auth`;
httpOptions = {
headers: new HttpHeaders({
'contentType': 'application/json'
})
}
constructor(private http: HttpClient) { }
signUp(userForm: User) {
return this.http.post(`${this.url}/signUp`, userForm, this.httpOptions)}
Angular Экологический код
export const environment = {
production: false,
apiUrl: 'http://localhost:3000'
};
Angular Код отправки формы компонента
onSignup() {
this.userForm = this.registerForm.value;
console.log(this.userForm)
this._signupService.signUp(this.userForm).subscribe(res => {
console.log(res)
}),
err => {console.log(err)}
this.registerForm.reset();
this.pwdstatus="";
}