Solution 1 :

[Cause Description]
Did not handle different request or set static resource.
All requests which came in your server would respond index.html.

enter image description here

You can add console log in the createServer function. Thus you would find that when the req.url is test.js you still return ./index.html to the request.

const server = http.createServer(function (req, res) {
    console.log(req.url)
    res.writeHead(200, { 'Content-Type': 'text/html' });
    fs.readFile('../index.html', function(error, data) {
        if(error) {
            res.writeHead(404);
            res.writeHead('Error: File Not Found');
        } else {
            res.write(data);
        }
        res.end();
    })
});

[Improvement]
Make sure when request url contain .js (or others), it will return correct response.

const server = http.createServer(function (req, res) {
    // fs.readFile('../index.html', function(error, data) {
    fs.readFile(__dirname + req.url, function (err,data) {
        if(error) {

https://nodejs.org/en/knowledge/HTTP/servers/how-to-serve-static-files/

Problem :

I am pretty new to webdev as a whole and this was my first time trying to set up a local server with Node. The issue though is I am getting “SyntaxError: Unexpected token ‘<‘ (anonymous function)” in my test.js line 1. I know it isn’t actually in line 1 because there is no code in line 1. Furthermore, my javascript file, html file and, my server js file are all very basic and I have no idea how they could go wrong.

here’s my test.js file



console.log('*');

here’s my index.html file

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>Test</h1>
</body>
<script type="text/javascript" src = "test.js"></script>
</html>

and here’s my server file that I run with “node server.js”

const http = require('http');
const fs = require('fs');

const port = 3000;

const server = http.createServer(function (req, res) {
    res.writeHead(200, { 'Content-Type': 'text/html' });
    fs.readFile('../index.html', function(error, data) {
        if(error) {
            res.writeHead(404);
            res.writeHead('Error: File Not Found');
        } else {
            res.write(data);
        }
        res.end();
    })
});

server.listen(port, function(error) {
    if(error) {
        console.log('something went wrong', error);
    } else {
        console.log('Server is listenting on port ' + port);
    }
});

as far as I understand, this is the most basic, cookie-cutter code I could use to test out whether my local server, the html file, and the test.js file are all working together well but obviously something is going wrong. When I click on the error in the console, it shows my index.html file but says it is my test.js file almost as if the html is being read in place of my javascript and the javascript error handling is being applied to it. I have checked the paths a few times and I am 99.99% sure they are correct. Im clueless on this one though, let me know if any of you have ran in to something similar. Thanks!

Comments

Comment posted by Adam Menker

hey thanks for the response, I’ve read the link and I am still a little confused, could you give me some guidance on what in my code is wrong or what I am missing?

Comment posted by John Lin

Though, I am not sure what is your project structure, i added some example code.

By

Leave a Reply

Your email address will not be published. Required fields are marked *