Menu

  • Home
  • Javascript (JS)
  • Node JS
  • React JS
  • React Native
  • More
    • Electron JS
    • WordPress
    • About
    • Code Examples
  • Contact Us
  • Our Services
  • +237 673917344
  • info@alljavascript.net
  • New Content Weekly
Beyond JavaScript Beyond JavaScript
  • Home
  • Javascript (JS)
  • Node JS
  • React JS
  • React Native
  • More
    • Electron JS
    • WordPress
    • About
    • Code Examples
  • Contact Us
  • Our Services
  • July 21, 2022
  • admin
  • 0 Comments
  • 177 Views
  • 0 Likes
  • Node JS

How to send Json response in express js

Usually, API calls or request are handled by sending a response using res.send(…). You can equally send a json response using the in-built res.json() method.
This method accepts an object and converts it to json during response.

read also: morgan http request logger

Code example:

Below is a simple express app with one route running on port 3000.
The home route sends a json response.

const express = require('express');
const app = express();

const data = { name: 'john', age: 23, city: 'Douala', email: 'test@test.com' }

app.get('/', (req, res) => {
    res.json(data)  // Object can also be passed in the brackets directly 
})

app.listen(3000, () => {
    console.log('code running on port 3000');
})

Output:

Thanks for coding with me..?
Hope you learnt something new. You can always drop your comments and share with other devs. Remember we will cover more real code and projects in the next tutorials.

Links: Express Website, JSON

Spread the love
Tags:
JsonNode JS
Prev PostHow to log Node JS http requests using morgan
Next PostWhat are Mongoose Hooks
Related Posts
  • How to Make API Calls in Electron.js: A Step-by-Step Tutorial February 7, 2024
  • How to Open and Read an Excel File in Node.js: A Step-by-Step Tutorial February 7, 2024

Leave a Comment Cancel Comment

Your email address will not be published. Required fields are marked *

Recent Posts

  • How to Make API Calls in Electron.js: A Step-by-Step Tutorial
  • How to Open and Read an Excel File in Node.js: A Step-by-Step Tutorial
  • Electron.js and Express.js backend: A Comprehensive Tutorial
  • How to Get Started with Electron.js: A Tutorial for Beginners
  • How to add Favicon in Express.js: A Step-by-Step Tutorial

Recent Comments

No comments to show.

Popular Posts

February 7, 2024 / Electron JS, Node JS
How to Make API Calls in Electron.js: A Step-by-Step Tutorial
February 7, 2024 / Javascript (JS), Node JS
How to Open and Read an Excel File in Node.js: A Step-by-Step Tutorial
February 7, 2024 / Electron JS, Javascript (JS)
Electron.js and Express.js backend: A Comprehensive Tutorial

Tags

Express Js Json Morgan Node JS NPM

Categories

  • Electron JS
  • Html, CSS
  • Javascript (JS)
  • MongoDB
  • Node JS
  • React Native

We are a community of developers aimed at teaching code and solving coding problems. We provide coding articles, opportunities, content, videos and tutorials to help developers around the world...

Our Services

  • Website Design
  • Domain and Web Hosting
  • Tutoring
  • Mobile Apps
  • Website Content Writing

Recent Posts

  • How to Make API Calls in Electron.j

    Feb 7, 2024

  • How to Open and Read an Excel File

    Feb 7, 2024

Useful Links

  • Home
  • Services
  • Blog #2
  • About Us
  • Contact Us
  • Request Tutorials
  • Jobs
  • Volunteer
  • Hire Us
  • Support
info@alljavascript.net Drop Us a Line
+237 673917344 Call Us Now
Africa Get Direction
Copyright 2022 alljavascript.net, All rights reserved.