I am new to Angular, APIs, node.js and coding in general. I have my API that only includes a POST request that works fine in Postman. I am trying to integrate it to an Angular app just for testing, but I am stuck, and a little help would be most appreciated. What I want to do is very simple. A user can enter a string that contains different types of parentheses (or none at all), and the API returns whether the entry is valid or not depending on whether the parentheses are balanced or not.
But I only get null
as a response, and I am not sure how to approach this.
Here are my codes.
1/ First my API:
const parenthesesChecker = require('./checker.js');
const express = require('express'), bodyParser = require('body-parser');
const router = express.Router();
router.use(bodyParser.json());
router.post('/', (req, res, next) => {
const stringCheck = ({
string: req.body
});
if (parenthesesChecker(stringCheck.string.string).result === true) {
res.status(200).json({
"isValid": true
});
} else {
res.status(400).json({
"isValid": false,
"errorDetails": {
"preceedingParentheses": `${parenthesesChecker(stringCheck.string.string).preceeding}`,
"erroneosParentheses": `${parenthesesChecker(stringCheck.string.string).erroneos}`
}
});
};
});
module.exports = router;
2/ Here is the Javascript function the API is calling:
module.exports = function (string) {
const openArray = ["(", "[", "{", "<"];
const closeArray = [")", "]", "}", ">"];
let i = 0, parOpen = 0, openingPar= "", closingPar = [], erroneos = "";
for(i; i < string.length; i++) {
if (openArray.includes(string[i])) {
openingPar += string[i];
parOpen++;
closingPar.push(closeArray[openArray.indexOf(string[i])]);
}
if (closeArray.includes(string[i])) {
let erroneos = string[i];
parOpen--;
if (string[i] === closingPar[closingPar.length - 1]) {
closingPar.pop();
} else {
return { result: false, preceeding: openingPar, erroneos: erroneos };
};
};
if (parOpen < 0) return { result: false, preceeding: openingPar, erroneos: erroneos };
}
return { result: (parOpen === 0 ? true : false), preceeding: openingPar, erroneos: erroneos };
};
Now let's move to the Angular part:
3/ The form, and the result that I wish to display in my app.components.html
file
<div>
<form>
<input #textEnter placeholder="text">
<button type="submit" (click)="onSubmit(textEnter.value)">click here</button>
</form>
<pre>
{{ (result | async) | json }}
</pre>
</div>
4/ My app.components.ts
file:
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Enter your String bellow:';
result;
test = "hello";
constructor(private http: HttpClient) {};
onSubmit(textEnter: string) {
const json = {
"string": textEnter
};
console.log(json);
this.http.post('http://localhost:3000/parentheses', json).toPromise().then((data:any) => {
this.result = data.json;
});
}
}
I guess that is all you need, but let me know if you want to see more stuff.
It seems to be working to some extent because when I run my API with npm start
and I pass in a string instead of a json, I get an error message in the terminal.
Also, as you can see I console.log(json);
in the app.component.ts
file. It does appear in the console, but super briefly, why is that?
Looking forward to anything that can help me move forward, I have been at it almost all day! Thanks!