You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/tutorials/basic_web_development/index.html
+22-18Lines changed: 22 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -115,21 +115,23 @@ <h2>Basic Web Development</h2>
115
115
file to specify the libraries we want to use, and a couple of folders: one
116
116
for CSS files and one for your source code.</p><p>The <code>deps.edn</code> file should have the following contents:</p><pre><codeclass="clojure">{:paths ["src" "resources"]
</code></pre><p>Now we'll create the first version of our source file:</p><pre><codeclass="clojure">;; this file is: src/my_webapp/handler.clj
134
136
(ns my-webapp.handler
135
137
(:require [compojure.core :refer [defroutes GET]]
@@ -149,10 +151,10 @@ <h2>Basic Web Development</h2>
149
151
(jetty/run-jetty #'app {:port 3000}))
150
152
</code></pre><blockquote><p>REPL-friendly code: we use the <code>#'</code> prefix on var names so that we can update the definitions while the program is running, without needing to restart our program -- see <ahref="https://clojure.org/guides/repl/enhancing_your_repl_workflow#writing-repl-friendly-programs">writing REPL-friendly programs on clojure.org</a>.</p></blockquote><blockquote><p>Note: the directory has an underscore in it (<code>my_webapp</code>) but the namespace has a hyphen in it (<code>my-webapp</code>). This is important in Clojure: we use lowercase names with hyphens to separate "words" -- often called kebab-case -- but the corresponding directory and filenames should be lowercase with underscores to separate "words" -- often called snake_case. This is due to how Clojure maps code onto names that are acceptable to the underlying JVM ecosystem.</p></blockquote><p>At this point you can run this very basic web application from the command-line:</p><pre><codeclass="bash">clojure -M -m my-webapp.handler
151
153
</code></pre><p>This says we want to run <ahref="https://clojure.org/reference/repl_and_main">Clojure's main entry point</a> (<code>-M</code>) and then <code>-m my-webapp.handler</code>
152
-
tells Clojure that we want it to run the <code>-main</code> function in that namespace.</p><p>It will output something like this (and then "hang" while the web server is running):</p><pre><code>2023-03-24 14:03:21.305:INFO::main: Logging initialized @2337ms to org.eclipse.jetty.util.log.StdErrLog
2023-03-24 14:03:21.783:INFO:oejs.AbstractConnector:main: Started ServerConnector@43201f84{HTTP/1.1, (http/1.1)}{0.0.0.0:3000}
155
-
2023-03-24 14:03:21.783:INFO:oejs.Server:main: Started @2815ms
154
+
tells Clojure that we want it to run the <code>-main</code> function in that namespace.</p><p>It will output something like this (and then "hang" while the web server is running):</p><pre><code>[main] INFO org.eclipse.jetty.server.Server - jetty-11.0.21; built: 2024-05-14T03:19:28.958Z; git: 996cd61addad9cb033e0e3eba6fa3f0fa3dc270d; jvm 21.0.1+12-LTS
155
+
[main] INFO org.eclipse.jetty.server.handler.ContextHandler - Started o.e.j.s.ServletContextHandler@677274e7{/,null,AVAILABLE}
156
+
[main] INFO org.eclipse.jetty.server.AbstractConnector - Started ServerConnector@5570ee6d{HTTP/1.1, (http/1.1)}{0.0.0.0:3000}
157
+
[main] INFO org.eclipse.jetty.server.Server - Started Server@729d1428{STARTING}[11.0.21,sto=0] @4321ms
156
158
</code></pre><blockquote><p>Note: you can stop this program running by pressing <code>^C</code> (control-c) on macOS or Linux, or by pressing <code>^Z</code> (control-z) on Windows.</p></blockquote><p>The only relevant line in that output is <code>Started ServerConnector</code> where it
157
159
shows the host and port it is running on -- <code>0.0.0.0:3000</code> -- so you should
158
160
be able to open a web browser and go to http://localhost:3000 and you should
@@ -398,28 +400,30 @@ <h2>Basic Web Development</h2>
398
400
<ahref="https://clojure.org/guides/tools_build"><code>tools.build</code> guide</a> for more details.</p><p>To make your webapp suitable for deployment, make the following
399
401
changes:</p><h3id="changes-in-depsedn">Changes in deps.edn</h3><p>In your <code>dep.edn</code> file add the following, after the <code>:deps</code> hash map:</p><pre><codeclass="clojure"> :aliases
</code></pre><h3id="add-a-buildclj-file">Add a build.clj file</h3><p>The <code>tools.build</code> library is intended to be used with a <code>build.clj</code> script
425
429
which typically lives in the root of your project and is invoked via the
0 commit comments