[{"data":1,"prerenderedAt":496},["ShallowReactive",2],{"\u002Farticles\u002Fbuilding-your-first-api-with-expressjs-a-beginners-guide":3},{"id":4,"title":5,"body":6,"description":487,"extension":488,"meta":489,"navigation":224,"path":491,"published":492,"seo":493,"stem":494,"__hash__":495},"articles\u002Farticles\u002Fbuilding-your-first-api-with-expressjs-a-beginners-guide.md","Building Your First API with Express.js: A Beginner's Guide",{"type":7,"value":8,"toc":476},"minimark",[9,16,21,24,28,31,34,78,81,85,88,146,150,153,331,334,338,341,394,398,401,404,438,448,452,455,459,462,465,469,472],[10,11,12],"blockquote",{},[13,14,15],"p",{},"This article was created using ChatGPT and meant as a placeholder",[17,18,20],"h2",{"id":19},"what-is-expressjs","What is Express.js?",[13,22,23],{},"Express.js is a minimal and flexible Node.js web application framework that provides a robust set of features to develop web and mobile applications. It facilitates the rapid development of Node-based web applications and is widely used to build APIs due to its simplicity and performance.",[17,25,27],{"id":26},"step-1-setting-up-your-environment","Step 1: Setting Up Your Environment",[13,29,30],{},"Before you start, ensure that you have Node.js installed on your system. You can download it from Node.js official website.",[13,32,33],{},"Once Node.js is installed, you can initiate your project:",[35,36,41],"pre",{"className":37,"code":38,"language":39,"meta":40,"style":40},"language-bash shiki shiki-themes github-light","mkdir my-express-api\ncd my-express-api\nnpm init -y\n","bash","",[42,43,44,57,66],"code",{"__ignoreMap":40},[45,46,49,53],"span",{"class":47,"line":48},"line",1,[45,50,52],{"class":51},"s7eDp","mkdir",[45,54,56],{"class":55},"sYBdl"," my-express-api\n",[45,58,60,64],{"class":47,"line":59},2,[45,61,63],{"class":62},"sYu0t","cd",[45,65,56],{"class":55},[45,67,69,72,75],{"class":47,"line":68},3,[45,70,71],{"class":51},"npm",[45,73,74],{"class":55}," init",[45,76,77],{"class":62}," -y\n",[13,79,80],{},"This creates a new directory for your project and initializes a new Node.js project.",[17,82,84],{"id":83},"step-2-installing-expressjs","Step 2: Installing Express.js",[13,86,87],{},"Install Express.js using npm (Node Package Manager):",[35,89,91],{"className":37,"code":90,"language":39,"meta":40,"style":40},"Copy code\nnpm install express --save\nThis command installs Express.js and adds it to your project's dependencies.\n",[42,92,93,101,114],{"__ignoreMap":40},[45,94,95,98],{"class":47,"line":48},[45,96,97],{"class":51},"Copy",[45,99,100],{"class":55}," code\n",[45,102,103,105,108,111],{"class":47,"line":59},[45,104,71],{"class":51},[45,106,107],{"class":55}," install",[45,109,110],{"class":55}," express",[45,112,113],{"class":62}," --save\n",[45,115,116,119,122,125,128,131,134,137,140,143],{"class":47,"line":68},[45,117,118],{"class":51},"This",[45,120,121],{"class":55}," command",[45,123,124],{"class":55}," installs",[45,126,127],{"class":55}," Express.js",[45,129,130],{"class":55}," and",[45,132,133],{"class":55}," adds",[45,135,136],{"class":55}," it",[45,138,139],{"class":55}," to",[45,141,142],{"class":55}," your",[45,144,145],{"class":55}," project's dependencies.\n",[17,147,149],{"id":148},"step-3-creating-your-first-express-server","Step 3: Creating Your First Express Server",[13,151,152],{},"Create a file named app.js in your project directory. This file will be the entry point of your API. Add the following code to app.js:",[35,154,158],{"className":155,"code":156,"language":157,"meta":40,"style":40},"language-js shiki shiki-themes github-light","Copy code\nconst express = require('express');\nconst app = express();\nconst port = 3000;\n\napp.get('\u002F', (req, res) => {\n  res.send('Hello World!');\n});\n\napp.listen(port, () => {\n  console.log(`Example app listening at http:\u002F\u002Flocalhost:${port}`);\n});\n","js",[42,159,160,166,189,203,219,226,262,278,284,289,304,326],{"__ignoreMap":40},[45,161,162],{"class":47,"line":48},[45,163,165],{"class":164},"sgsFI","Copy code\n",[45,167,168,172,174,177,180,183,186],{"class":47,"line":59},[45,169,171],{"class":170},"sD7c4","const",[45,173,110],{"class":62},[45,175,176],{"class":170}," =",[45,178,179],{"class":51}," require",[45,181,182],{"class":164},"(",[45,184,185],{"class":55},"'express'",[45,187,188],{"class":164},");\n",[45,190,191,193,196,198,200],{"class":47,"line":68},[45,192,171],{"class":170},[45,194,195],{"class":62}," app",[45,197,176],{"class":170},[45,199,110],{"class":51},[45,201,202],{"class":164},"();\n",[45,204,206,208,211,213,216],{"class":47,"line":205},4,[45,207,171],{"class":170},[45,209,210],{"class":62}," port",[45,212,176],{"class":170},[45,214,215],{"class":62}," 3000",[45,217,218],{"class":164},";\n",[45,220,222],{"class":47,"line":221},5,[45,223,225],{"emptyLinePlaceholder":224},true,"\n",[45,227,229,232,235,237,240,243,247,250,253,256,259],{"class":47,"line":228},6,[45,230,231],{"class":164},"app.",[45,233,234],{"class":51},"get",[45,236,182],{"class":164},[45,238,239],{"class":55},"'\u002F'",[45,241,242],{"class":164},", (",[45,244,246],{"class":245},"sqxcx","req",[45,248,249],{"class":164},", ",[45,251,252],{"class":245},"res",[45,254,255],{"class":164},") ",[45,257,258],{"class":170},"=>",[45,260,261],{"class":164}," {\n",[45,263,265,268,271,273,276],{"class":47,"line":264},7,[45,266,267],{"class":164},"  res.",[45,269,270],{"class":51},"send",[45,272,182],{"class":164},[45,274,275],{"class":55},"'Hello World!'",[45,277,188],{"class":164},[45,279,281],{"class":47,"line":280},8,[45,282,283],{"class":164},"});\n",[45,285,287],{"class":47,"line":286},9,[45,288,225],{"emptyLinePlaceholder":224},[45,290,292,294,297,300,302],{"class":47,"line":291},10,[45,293,231],{"class":164},[45,295,296],{"class":51},"listen",[45,298,299],{"class":164},"(port, () ",[45,301,258],{"class":170},[45,303,261],{"class":164},[45,305,307,310,313,315,318,321,324],{"class":47,"line":306},11,[45,308,309],{"class":164},"  console.",[45,311,312],{"class":51},"log",[45,314,182],{"class":164},[45,316,317],{"class":55},"`Example app listening at http:\u002F\u002Flocalhost:${",[45,319,320],{"class":164},"port",[45,322,323],{"class":55},"}`",[45,325,188],{"class":164},[45,327,329],{"class":47,"line":328},12,[45,330,283],{"class":164},[13,332,333],{},"This code creates a basic Express server that listens on port 3000 and responds with \"Hello World!\" to HTTP GET requests to the root URL (\u002F).",[17,335,337],{"id":336},"step-4-running-your-express-server","Step 4: Running Your Express Server",[13,339,340],{},"Run your server using Node.js:",[35,342,344],{"className":37,"code":343,"language":39,"meta":40,"style":40},"Copy code\nnode app.js\nVisit http:\u002F\u002Flocalhost:3000 in your browser. You should see the message \"Hello World!\".\n",[42,345,346,352,360],{"__ignoreMap":40},[45,347,348,350],{"class":47,"line":48},[45,349,97],{"class":51},[45,351,100],{"class":55},[45,353,354,357],{"class":47,"line":59},[45,355,356],{"class":51},"node",[45,358,359],{"class":55}," app.js\n",[45,361,362,365,368,371,373,376,379,382,385,388,391],{"class":47,"line":68},[45,363,364],{"class":51},"Visit",[45,366,367],{"class":55}," http:\u002F\u002Flocalhost:3000",[45,369,370],{"class":55}," in",[45,372,142],{"class":55},[45,374,375],{"class":55}," browser.",[45,377,378],{"class":55}," You",[45,380,381],{"class":55}," should",[45,383,384],{"class":55}," see",[45,386,387],{"class":55}," the",[45,389,390],{"class":55}," message",[45,392,393],{"class":55}," \"Hello World!\".\n",[17,395,397],{"id":396},"step-5-building-a-simple-api","Step 5: Building a Simple API",[13,399,400],{},"Now, let's expand our server to act as a simple API. For example, let's create an endpoint that returns a list of users.",[13,402,403],{},"Add the following code to your app.js:",[35,405,409],{"className":406,"code":407,"language":408,"meta":40,"style":40},"language-javascript shiki shiki-themes github-light","Copy code\nlet users = [{ name: \"Alice\" }, { name: \"Bob\" }];\n\napp.get('\u002Fusers', (req, res) => {\n  res.json(users);\n});\n","javascript",[42,410,411,415,420,424,429,434],{"__ignoreMap":40},[45,412,413],{"class":47,"line":48},[45,414,165],{},[45,416,417],{"class":47,"line":59},[45,418,419],{},"let users = [{ name: \"Alice\" }, { name: \"Bob\" }];\n",[45,421,422],{"class":47,"line":68},[45,423,225],{"emptyLinePlaceholder":224},[45,425,426],{"class":47,"line":205},[45,427,428],{},"app.get('\u002Fusers', (req, res) => {\n",[45,430,431],{"class":47,"line":221},[45,432,433],{},"  res.json(users);\n",[45,435,436],{"class":47,"line":228},[45,437,283],{},[13,439,440,441,447],{},"Now, if you visit ",[442,443,444],"a",{"href":444,"rel":445},"http:\u002F\u002Flocalhost:3000\u002Fusers",[446],"nofollow",", you will see the JSON representation of the users array.",[17,449,451],{"id":450},"step-6-testing-your-api","Step 6: Testing Your API",[13,453,454],{},"It’s important to test your API. You can use tools like Postman or curl to test your endpoints.",[17,456,458],{"id":457},"step-7-next-steps","Step 7: Next Steps",[13,460,461],{},"From here, you can start building more complex APIs. Consider the following:",[13,463,464],{},"Implementing CRUD (Create, Read, Update, Delete) operations.\nConnecting your API to a database.\nAdding authentication and authorization.\nOrganizing your code with routers and controllers.",[17,466,468],{"id":467},"conclusion","Conclusion",[13,470,471],{},"Express.js simplifies the process of building APIs in Node.js. It's a great starting point for developers looking to delve into backend development. With its minimalist approach, you have the freedom to structure your applications as you see fit, making Express.js an invaluable tool in your development toolkit.",[473,474,475],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html pre.shiki code .s7eDp, html code.shiki .s7eDp{--shiki-default:#6F42C1}html pre.shiki code .sYBdl, html code.shiki .sYBdl{--shiki-default:#032F62}html pre.shiki code .sYu0t, html code.shiki .sYu0t{--shiki-default:#005CC5}html pre.shiki code .sgsFI, html code.shiki .sgsFI{--shiki-default:#24292E}html pre.shiki code .sD7c4, html code.shiki .sD7c4{--shiki-default:#D73A49}html pre.shiki code .sqxcx, html code.shiki .sqxcx{--shiki-default:#E36209}",{"title":40,"searchDepth":59,"depth":59,"links":477},[478,479,480,481,482,483,484,485,486],{"id":19,"depth":59,"text":20},{"id":26,"depth":59,"text":27},{"id":83,"depth":59,"text":84},{"id":148,"depth":59,"text":149},{"id":336,"depth":59,"text":337},{"id":396,"depth":59,"text":397},{"id":450,"depth":59,"text":451},{"id":457,"depth":59,"text":458},{"id":467,"depth":59,"text":468},"A beginner-friendly guide to building your first API with Express.js","md",{"slug":490},"building-your-first-api-with-expressjs-a-beginners-guide","\u002Farticles\u002Fbuilding-your-first-api-with-expressjs-a-beginners-guide","2023-11-02",{"title":5,"description":487},"articles\u002Fbuilding-your-first-api-with-expressjs-a-beginners-guide","Kq7PWZeOMPaj7CoqIGHFyWZ8fEBP6q6dSyUwgIipgc4",1780299452029]