Using fetch() to Upload a File

File uploads are trickier than it seems. There are a few ways to do it on the client side. It really is just sending a POST/PUT request to the server you’re uploading to, but set the content type to multipart/form-data and the body is no longer strictly strings or JSON, but binary files, or any format you desire. One way to do it to use a form submission, which looks like this:

Continue reading Using fetch() to Upload a File

Simple File Upload with Express.js and Multer in Node.js

A couple of years ago I wrote a blog post on how to upload a file with formidable. I had the chance to write the upload function again, and found multer to be much easier to use. Here’s another beginner example.

This example saves your file into the file system.

Read the multer documentation to do different kinds of uploads.

Continue reading Simple File Upload with Express.js and Multer in Node.js

Bounce Some Particles with three.js

I gave a talk at JSConf China, for which I made a little demo in three.js. I wanted to be able to explain every line of code in ~10 minutes to people with no graphics knowledge. JSConf China is not a multi-track conference, everyone attending the conference hears the talk so I stuck to the basics.

Btw, the slides to my talk is here.

particles

Demo with animation and controls

The demo was extremely simple. It basically:
1. creates a three.js scene with init()
2. adds some spheres to the scene with fillScene()
3. animate and add controls to the scene with animate()
Continue reading Bounce Some Particles with three.js

Parsing Forms with Multiple Submit Buttons in Node.js with Express 4

Here is a very simple, bare minimum example to demonstrate how Express 4 and body-parser module.
I’m using ejs as the rendering engine because it looks like html.

Setup

Continue reading Parsing Forms with Multiple Submit Buttons in Node.js with Express 4

Simple File Upload with Express.js and Formidable in Node.js

Here’s a very simple upload example with express 4+ and formidable. According to Express doc:

In Express 4, req.files is no longer available on the req object by default. To access uploaded files on the req.files object, use a multipart-handling middleware like busboy, multer, formidable, multiparty, connect-multiparty, or pez.

So here’s an example combining the two.

The code is going to save the files you upload in disk under the folder /uploads in the root directory of the project. Continue reading Simple File Upload with Express.js and Formidable in Node.js

Send HTTP Requests To a Server with Node.js

It’s actually incredibly easy to send http/https requests to servers with Node.js. Node contains a bunch of native function for that, making it very easy to send REST requests. I’m going to demonstrate with Node’s own http.request(options[, callback]) method.

http-between-servers

HTTP/HTTPS requests in a very brief glance

The four most common HTTP requests are GET, POST, PUT, DELETE. The type of request determines the type of operation the request sender would like the server to perform.

Continue reading Send HTTP Requests To a Server with Node.js

Autodesk View and Data API: Rendering 3D in Your Browser

Before I dig in any more deeper to how to write apps with Autodesk’s APIs I’d like to show some demos and what problem(s) it’s solving.

Why we need to render 3D models in a browser

Autodesk is a computer aided design (CAD) software company. The company was founded with the product AutoCAD, still one of the most, if not the most influential CAD software. From then on it also offered many other software for more specific domain. E.g. Revit for architectural design, Inventor or Fusion for industrial design, Maya or 3ds Max for special effects in movies, etc.

If you’re not in this space, just think that most 3D designed objects, like the Herman Miller Aeron chair, or the San Francisco Bay Bridge, or Pixar movies, have some involvement with some Autodesk software. CAD is everywhere, it’s in buildings, it’s in computers, it’s in furniture, it’s in movies, it’s in every engineering student’s curriculum. In every single domain, there’s one or more different CAD software that caters exactly towards the industry for design. There’s a gazillion number of CAD software, from Autodesk and many other companies.

The problem with a gazillion of CAD software is that there are a gazillion of file formats they export to. When people in different domains have to work with each other, like in constructing a new building, the use case is often very clunky. When a new building is being constructed, there are architects that design the building, structural engineers to make sure the building won’t tip over, mechanical engineers who design the heating, ventilation and air conditioning (HVAC) system and plumbing system, electrical engineers that design the wiring, then there’s engineers that put everything together to make sure a electrical wires don’t go right through a water pipe. Every single team use a software of their choice that exports to different file formats.

There’s another problem: CAD software is very expensive. It’s in the thousand dollar per year range. So the person who created the design has no cheap way to give their work to their client, if they’re not looking at the same computer screen.

This is where the View and Data API comes in.
Continue reading Autodesk View and Data API: Rendering 3D in Your Browser

How To: Request an Access Token for Autodesk APIs with Node.js

In this post I’ll demonstrate how to set up a simple Node.js server that will return an access token for Autodesk’s Web Service APIs. Every time you run the app, you’ll get a response in your console that contains the token.

Authentication API

Authentication API(link goes to official documentation) takes a POST request that contains your client id and secret in the request body. It sends you an access token that would expire in 1799 seconds – that is 30 minutes, to perform operations with Autodesk’s Web Service APIs.

The request:

POST /authentication/v1/authenticate HTTP/1.1
Host: developer.api.autodesk.com
Content-Type: application/x-www-form-urlencoded
client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=client_credentials

The response body should look like this:

{
  "token_type": "Bearer",
  "expires_in": 1799,
  "access_token": "Ttj3qdRiNRGQsu975PPXF578hzmP"
}

Now that we understood what the API does, let’s get started! If you’d like to skip the tutorial, all the code is in a GitHub repo.

Continue reading How To: Request an Access Token for Autodesk APIs with Node.js