Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Express Basics (2015) Developing Express Apps Like a Boss Adding Routes to the App

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

Can you only use JSON files in your mock folder

I can't find any information on this, but when I was catching up on this course and tried to use get.send on the blog route to send a simple text file to the server the application failed.

What kind of files can I use with get.send other than JSON files? Thanks :)

'use strict';
//import express modile with require
var express = require('express'),
     posts = require('./mock/blogposts.txt');

//assign express to a variable
var app = express();

//location parameter with get variable with an added anonymous callback function.
app.get('/', function(req, res){
  res.send("<h2>i am a html string</h2>");
});


app.get('/blog', function(req, res) {
     res.send(posts);
});

console.log("A connection to port 3000 is now open");
//listen for an HTTP local server on port 3000
app.listen(3000);

1 Answer

rydavim
rydavim
18,813 Points

I haven't done the Express course yet, but I found some documentation that might help you.

res.send([body]) - The body parameter can be a Buffer object, a String, an object, or an Array.

It sounds like maybe res.sendFile(path [, options] [, fn]) might be what you're looking for?

res.sendFile(path [, options] [, fn]) transfers the file at the given path. Sets the Content-Type response HTTP header field based on the filename’s extension. Unless the root option is set in the options object, path must be an absolute path to the file.

Here is the res.send documentation I was looking at, and here is the res.sendFile documentation. Hopefully that helps you find what you're looking for. Happy coding!

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

Thanks, I'm looking forward to having a play with this later on. Will let you know how it goes :)

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

from the looks of it, it's about file transferrring betweeen different servers which isn't quite what I'm after. I just want to display the contents of the file to the browser which I can only seem to do with send() as a JSON file rather than a plain text. :-)