Nodejs, I'm asking you a question.

Asked 1 years ago, Updated 1 years ago, 102 views

``Hello, I'm going to print out a video using node js express. I set the path well, but the video didn't run, so I'm posting a question.

This is the main app.js.

The express module was used to output the html format, video.js.

You set the path well, but why isn't it working?

These are the codes.

app.js

varcreateError=require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var videoRouter = require('./routes/video');

var app = express();

// // view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/video', videoRouter);

// // catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// // error handler
app.use(function(err, req, res, next) {
  // // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

video.js

var express = require('express');
var fs = require('fs');
var router = express.Router();

/* /* GET home page. */
router.get('/', function(req, res, next) {
  res.render('video', { title: 'video' });
});

module.exports = router;

video.ejs

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8"/>
    <title><%= title %></title>
</head>
<body>
    <h1>><%= title %></h1>

    <video width='400' height="400" autoplay controls loop>
        <source src='video/Squat.mp4'>

    </video>
    Run the video
</body>
</html>

node.js ejs html express

2022-09-22 19:42

1 Answers

HTTP 404 is an "Unfindable" error. Where have you been looking to find it? This source is looking for the /video/Squat.mp4 file in the /public folder.
If you look at app.js, it looks like this.

app.use(express.static(path.join(__dirname, 'public')));

Leave the source as it is now, create a video directory under the public folder, move the file, and try again. It'll probably come out.

Learn more about express.static()


2022-09-22 19:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.