user5096599
user5096599

Reputation:

app.get is not working

My backend is working for other files I have included in it, but these new files are in a folder called js in the directory. These are the only files that are in a folder, and they are not loading and I am not sure why. I am assuming it is just a syntax error but have not been able to figure it out after an hour.

on my html I have the following code:

<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
        <script type="text/javascript" src="js/TweenMax.min.js"></script>
        <script type="text/javascript" src="js/cooltext.animations.js"></script>
        <script type="text/javascript" src="js/cooltext.min.js"></script>

and in my app.js I have used the following code:

app.get('/js/jquery-1.10.2.min.js', function (req, res) {
    res.sendFile(__dirName + '/js/jquery-1.10.2.min.js');
});

app.get('/js/TweenMax.min.js', function (req, res) {
    res.sendFile(__dirName + '/js/TweenMax.min.js');
});

app.get('/js/cooltext.animations.js', function (req, res) {
    res.sendFile(__dirName + '/js/cooltext.animations.js');
});
app.get('/js/cooltext.min.js', function (req, res) {
    res.sendFile(__dirName + '/js/cooltext.min.js');
});

Upvotes: 0

Views: 484

Answers (2)

G&#246;kay G&#252;rcan
G&#246;kay G&#252;rcan

Reputation: 1094

Quoting from Express' docs: http://expressjs.com/starter/static-files.html


Serving static files in Express

Serving files, such as images, CSS, JavaScript and other static files is accomplished with the help of a built-in middleware in Express - express.static.

Pass the name of the directory, which is to be marked as the location of static assets, to the express.static middleware to start serving the files directly. For example, if you keep your images, CSS, and JavaScript files in a directory named public, you can do this:

app.use(express.static('public'));

Now, you will be able to load the files under the public directory:

http://localhost:3000/images/kitten.jpg
http://localhost:3000/css/style.css
http://localhost:3000/js/app.js
http://localhost:3000/images/bg.png
http://localhost:3000/hello.html

Upvotes: 0

user5096599
user5096599

Reputation:

I figured it out. I had to use:

app.use(express.static('js'));

and remove the 'js' from the links in the index.html.

Upvotes: 1

Related Questions