Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,028 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,798 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,028 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,028 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. :-)