I have 2 projects all in node, using express. Is there an easy way to migrate it?
What happens to all the imported modules and dependencies?
Can I still use express in Deno the same way?
"Easy" is highly subjective. If you're proficient at picking up new architecture paradigms for familiar languages, you're adept with ESM syntax, and refactoring is a skill; then you might consider it easy.
It's a paradigm shift, so there's always going to be some friction in migration. For your express needs, check out Oak https://deno.land/x/oak@v9.0.0
I wouldn't call it a paradigm shift in terms of the code itself. The main thing is that it's a completely different set of system APIs that have to be targeted (by you or by the libraries you use). That means a lot of changes wherever those are involved, but they should mostly be straightforward changes (of course YMMV if you're having to use a new library/framework instead of just the new system APIs)
This is my first day obviously, but what I can see are the following interesting syntax, bare server.
const server = Deno.listen({ port: 8080 });
console.log(`HTTP webserver running. Access it at: http://localhost:8080/`);
// Connections to the server will be yielded up as an async iterable.
for await (const conn of server) {
// In order to not be blocking, we need to handle each connection individually
// without awaiting the
function
serveHttp(conn);
}
async function serveHttp(conn: Deno.Conn) {
// This "upgrades" a network connection into an HTTP connection.
const httpConn = Deno.serveHttp(conn);
// Each request sent over the HTTP connection will be yielded as an async
// iterator from the HTTP connection.
for await (const requestEvent of httpConn) {
// The native HTTP server uses the web standard `Request` and `Response`
// objects.
const body = `Your user-agent is:\n\n${requestEvent.request.headers.get(
"user-agent",
) ?? "Unknown"}`;
// The requestEvent's `.respondWith()` method is how we send the response
// back to the client.
requestEvent.respondWith(
new Response(body, {
status: 200,
}),
);
}
}
Everything async await. There is an option to use a library, but it's to be deprecated,the native option is stable.
Interesting import code:
import { serve } from "https://deno.land/std@0.105.0/http/server.ts";
So, Deno supports typescript out of the box or just plain JS.
Deno doesn't support npm packages, imports are done via url.
There is a window object, for whatever that's supposed to be used.
Runs sandboxed, access to filesystem etc runs on permission basis.
Access to the browser API without installing anything else.
Packages are cached, this was very annoying in nodeJS.
package management, scripts are quite the shift in Deno. there are tools like velociraptor and trex which help bridge the gap, but there's still a lot to learn that's much different than node-world.
Probably not, but Deno has Oak, which is Express-like and the combination of those two will get you close. But most node libs are not compatible and similarly node projects.
Thanks for that,Oh dear, I was secretly hoping for something as easy as a new package.json.
A quick look around, I found this:
Opine attempts to solve this by completely porting ExpressJS over to TypeScript in Deno, making changes only where the Deno APIs
dramatically differ from Node.
Will give it a try, really curious how importing works and all.
So was I, so this is what I determined, on top of the official docs.[1]
On deno run of a script, it will download referenced imports and put them into a cache directory with a hash of the name. On a UNIX-like, this seems to be in ~/.cache/deno/deps (there's also a ~/.cache/deno/gen for the TS to JS stuff). Subsequent runs do not seem to download the scripts, but instead use the cached hashed versions. There's probably a way to blow out or overwrite the cache with newer download with a deno command, but I'm not sure what it is.
Compiling bundled the cached dependencies, so they aren't downloaded on run. Importing from local files is also possible, as noted in the docs I linked.
I'm happy to be corrected to supplemented on any of that info, I would rather know the actual way it works than persist in a slightly wrong assumption. :)
Deno is striving for more unified APIs with browsers, so while they are moving away from the "platform" of internal use only V8 APIs, they are moving toward the "web platform", which seems an admirable goal to me. One of my biggest complaints for a while has been how drastically different some Node APIs are from the way any actual "web platform" APIs work in 2021 (Promises for filesystem APIs in Node are still "experimental", and meanwhile there's an actual filesystem API standardized in the "web platform" that is entirely incompatible but Promise-based). You can polyfill some of the "web platform" libraries back on top of Node in many cases, but Deno seems sensible to me in trying to better unify such things from the start.
One of Deno's value props is to have a tighter and more explicit trust model.
And this is not a obscure need. There are cloud providers who let you run JS on a specialized and restricted API.
However, there is nothing stopping you from using Node or integrating V8 and go wild with it! (In fact I would argue that this is a solid strategy for some types of projects, where you can get the best of both (static/dynamic) worlds.
And this is not a obscure need. There are cloud providers who let you run JS on a specialized and restricted API.
Sounds rather obscure actually. Still somewhat confused why this would be useful in Deno. Looks like it's just a repeat of what happened with Java's SecurityManager, which is now being deprecated and removed.
Comments
I have 2 projects all in node, using express. Is there an easy way to migrate it? What happens to all the imported modules and dependencies? Can I still use express in Deno the same way?
"Easy" is highly subjective. If you're proficient at picking up new architecture paradigms for familiar languages, you're adept with ESM syntax, and refactoring is a skill; then you might consider it easy.
It's a paradigm shift, so there's always going to be some friction in migration. For your express needs, check out Oak https://deno.land/x/oak@v9.0.0
I wouldn't call it a paradigm shift in terms of the code itself. The main thing is that it's a completely different set of system APIs that have to be targeted (by you or by the libraries you use). That means a lot of changes wherever those are involved, but they should mostly be straightforward changes (of course YMMV if you're having to use a new library/framework instead of just the new system APIs)
Could you clarify what is the paradigm shift? Static types? (If so I agree, but using Node does not imply not using TS)
This is my first day obviously, but what I can see are the following interesting syntax, bare server.
} }Everything async await. There is an option to use a library, but it's to be deprecated,the native option is stable. Interesting import code:
So, Deno supports typescript out of the box or just plain JS. Deno doesn't support npm packages, imports are done via url. There is a window object, for whatever that's supposed to be used. Runs sandboxed, access to filesystem etc runs on permission basis. Access to the browser API without installing anything else. Packages are cached, this was very annoying in nodeJS.Sounds and looks pretty damn good.
Probably more the removal of nodejs apis in favour of browser apis. For everything. (modules/imports included).
package management, scripts are quite the shift in Deno. there are tools like velociraptor and trex which help bridge the gap, but there's still a lot to learn that's much different than node-world.
Probably not, but Deno has Oak, which is Express-like and the combination of those two will get you close. But most node libs are not compatible and similarly node projects.
Thanks for that,Oh dear, I was secretly hoping for something as easy as a new package.json. A quick look around, I found this: Opine attempts to solve this by completely porting ExpressJS over to TypeScript in Deno, making changes only where the Deno APIs dramatically differ from Node.
Will give it a try, really curious how importing works and all.
So was I, so this is what I determined, on top of the official docs.[1]
On deno run of a script, it will download referenced imports and put them into a cache directory with a hash of the name. On a UNIX-like, this seems to be in ~/.cache/deno/deps (there's also a ~/.cache/deno/gen for the TS to JS stuff). Subsequent runs do not seem to download the scripts, but instead use the cached hashed versions. There's probably a way to blow out or overwrite the cache with newer download with a deno command, but I'm not sure what it is.
Compiling bundled the cached dependencies, so they aren't downloaded on run. Importing from local files is also possible, as noted in the docs I linked.
I'm happy to be corrected to supplemented on any of that info, I would rather know the actual way it works than persist in a slightly wrong assumption. :)
1: https://deno.land/manual@v1.11.5/examples/import_export
Isn't there a compatibility layer available, or are there reasons that in principle make this impossible?
There is a compatibility layer but it is not complete.
One of Deno's objectives is not to expose V8 internals, so node packages relying on those will be harder to port
Who doesn't love being removed even further from the platform they're running on?
Deno is striving for more unified APIs with browsers, so while they are moving away from the "platform" of internal use only V8 APIs, they are moving toward the "web platform", which seems an admirable goal to me. One of my biggest complaints for a while has been how drastically different some Node APIs are from the way any actual "web platform" APIs work in 2021 (Promises for filesystem APIs in Node are still "experimental", and meanwhile there's an actual filesystem API standardized in the "web platform" that is entirely incompatible but Promise-based). You can polyfill some of the "web platform" libraries back on top of Node in many cases, but Deno seems sensible to me in trying to better unify such things from the start.
One of Deno's value props is to have a tighter and more explicit trust model.
And this is not a obscure need. There are cloud providers who let you run JS on a specialized and restricted API.
However, there is nothing stopping you from using Node or integrating V8 and go wild with it! (In fact I would argue that this is a solid strategy for some types of projects, where you can get the best of both (static/dynamic) worlds.
Sounds rather obscure actually. Still somewhat confused why this would be useful in Deno. Looks like it's just a repeat of what happened with Java's SecurityManager, which is now being deprecated and removed.
It is how cloudflare workers work
Yes, something very obscure. Maybe a handful of companies in the world have a need to operate something like that.