0
I’m following a video lesson and a question has arisen.
created two folders one for the front (which is run by http-server port 8080)
second folder to the back ( which is run by the port 3000 nodemon)
but unfortunately does not carry things back to front... follow my codes if someone can give me a help, please.
/*PASTA front arquivo main.js*/
(function () {
'use strict';
var ajax = new XMLHttpRequest();
ajax.open('GET', 'http://localhost:3000/user/maria');//esse trecho de código não altera nem com nome invalido que n tenha no objeto do js do back.
ajax.send();
ajax.addEventListener('readystatechange', function () {
if (ajax.readyState === 4) {
console.log(ajax.responseText, ajax.status);
}
}, false);
})();
/* PASTA back arquivo index.js */
'use strict';
var express = require('express');
var cors = require('cors');
var app = express();
app.use(cors());
var users = {
joao: {
nome: 'Joao',
idade: 30
},
maria: {
nome: 'Maria',
idade: 22
},
fernando: {
nome: 'Fernando',
idade: 20
}
};
app.get('/', function (request, response) {
response.send('<h1>Home</h1>');
});
app.get('/user', function (request, response) {
response.send('User');
});
app.get('/user/:username', function (request, response) {
var username = request.params.username;
if(users[username])
return response.json(users[username]);
response.status(404).json({ error: 'Usuário não encontrado'});
});
app.listen(3000);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> AJAX </title>
</head>
<body>
<h1> FRONT </h1>
<script scr="/js/main.js"></script>
</body>
</html>
But what happens ? Gives some error ? Nothing appears on the page ? Which of the urls does not work ? All ?
– Isac
Dei F12 no error appeared in the console. Nothing appeared in any of the urls. I saw that in Brackets there are some errors. var ajax = new Xmlhttprequest(); //claim of Xmlhttprequest() var express = require('express'); //claim of require var Cors = require('Cors'); executed the npm command -install g require install-install the error persist
– Bianca Maciel