node js express
basic server const express =require('express'); const app = express(); const PORT = 5000; app.get('/',(req,res)=>{ res.json({message: 'Welcome to the backend'}) }) app.listen(PORT ,()=>console.log(`Connected to ${PORT}`)
node js express
basic server const express =require('express'); const app = express(); const PORT = 5000; app.get('/',(req,res)=>{ res.json({message: 'Welcome to the backend'}) }) app.listen(PORT ,()=>console.log(`Connected to ${PORT}`)
node js express
// node js -> express -> basic example: static folder, 404 page const express = require('express'); const path = require('path'); const PORT = process.env.PORT || 5000; const app = express(); function error404(req, res) { res.status(404); if (req.accepts('html')) { res.sendFile(path.join(__dirname, 'public/errors/404.html')); return; } if (req.accepts('json')) { res.send({ status: 404, error: 'Not found' }); return; } res.type('txt').send('404 - Not found'); } app .use(express.static(path.join(__dirname, 'public'))) .use(error404) .listen(PORT, () => console.log(`Listening on ${ PORT }`));
require express server.js
const express = require('express')const app = express() app.get('/', function (req, res) { res.send('Hello World')}) app.listen(3000)
express render
// send the rendered view to the client res.render('index') // if a callback is specified, the rendered HTML string has to be sent explicitly res.render('index', function (err, html) { res.send(html) }) // pass a local variable to the view res.render('user', { name: 'Tobi' }, function (err, html) { // ... })
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us