Doubt loading the rest of the view

0

I'm following a video lesson and a question has come up.

I created two folders one for the front (which is run by the http-server port 8080 server)

second folder to the back (which is run by nodemon port 3000)

But unfortunately it does not load things back to the front ... follow my codes if anyone can give me a hand, 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>
    
asked by anonymous 21.06.2018 / 07:25

0 answers