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

General Discussion Previews and Snapshots

Previewing NodeJS workspace

Created a new NodeJS workspace. Created a index.html, added paragraph text. When I click the preview icon, page in the port I chose says "Workspace Unavailable". Why?

snapshot - https://w.trhou.se/jtobsd155f

6 Answers

Nathan Williams
seal-mask
.a{fill-rule:evenodd;}techdegree
Nathan Williams
Python Web Development Techdegree Student 6,851 Points

NodeJS workspaces aren't meant to work with static files, they're meant to allow you to preview a running nodejs backend server, so unless you have one running, you get an error page because there's no backend server listening to the port you specified.

Ok, guess I was expecting there to be web-based previews with workspaces.

Nathan Williams
seal-mask
.a{fill-rule:evenodd;}techdegree
Nathan Williams
Python Web Development Techdegree Student 6,851 Points

there are, if you pick an appropriate environment as your workspace type when you're creating it. Try picking the "Front End" environment.

We can't test out any interaction / connection between front-end and back-end environments in a web-based preview though?

*unless the backend is our own

Nathan Williams
seal-mask
.a{fill-rule:evenodd;}techdegree
Nathan Williams
Python Web Development Techdegree Student 6,851 Points

To confirm, you're thinking of something like running a nodejs api service listening on port 3000 or something, and serving a static HTML file over the default web port (80) with some javascript that talks to the nodejs server on port 3000?

I want to click a button in html and see how that can trigger a function in NodeJS.

Nathan Williams
seal-mask
.a{fill-rule:evenodd;}techdegree
Nathan Williams
Python Web Development Techdegree Student 6,851 Points

"trigger a function in NodeJS" on the client-side (in the browser), or server-side (on the backend)?

If the former, just use a Front End workspace environment, the NodeJS environment doesn't run a web-server to serve static HTML, so you'd have to run your own (with something like express); if the latter (frontend talking to a custom backend), I'll have to dig around a bit to see if that can be achieved with our current workspaces setup, or if we'd need to make some changes to workspaces.

I realize NodeJS doesn't run client-side. I'm missing how to bind front-end events to a NodeJS backend function and pass data to one too. That must not be a common requirement if an example is not implemented here already.

Nathan Williams
seal-mask
.a{fill-rule:evenodd;}techdegree
Nathan Williams
Python Web Development Techdegree Student 6,851 Points

Yeah, I can definitely see the use-case, I just don't think we've delivered a course that requires it yet; fwiw you can serve the frontend code via the backend service at a different route as-is, so there's a couple ways to achieve what you're aiming for. I'll open a feature request on our end to get a static webserver able to run in tandem with Workspaces based on the Node environment, but I can't say for sure when we may be able to make the requisite changes.