节点启动我的服务器,吞咽也是如此,但吞咽并没有找到任何路径 [英] Node starts my server, so does gulp, but gulp doesn't find any paths

查看:124
本文介绍了节点启动我的服务器,吞咽也是如此,但吞咽并没有找到任何路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信这是一件小事,但我无法弄清楚。 Node,nodemon和gulp使用app.js来启动我的服务器,所以我不确定为什么一个方法可以工作,而gulp不能。如果我使用以下方式启动我的服务器:

nodemon app.js或
节点app.js启动正常,并且在浏览器中使用localhost:1337路径它做它应该做的事。



当我使用命令时:



gulp



它表示它启动了服务器,但是当我导航到localhost:1337时,它不显示除Can not GET /之外的任何内容,并且我的所有源都未在检查器中显示。我仍然收到app正在监听消息,如我的app.js console.log中所示。我的大文件如下:

  var gulp = require('gulp'); 
var nodemon = require('gulp-nodemon');
$ b gulp.task('nodemon',function(){
nodemon({
script:'./app/app.js'
})

});
$ b $ gulp.task('watch',function(){
gulp.watch('*。js',['nodemon']);
});

gulp.task('default',['nodemon']);






  my app.js文件是:

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

app.use(express.static('../ views'));
app.use(express.static('../ bower_components / angular'));
app.use(express.static('./ directives'));

app.listen(1337);
console.log(The port is listening);


解决方案

当对未定义的路由发出请求时, localhost:1337 是Express的通用消息。需要为要提供的页面定义路由。您需要在 http:// localhost:1337 / 中添加一个Route来处理对您的应用程序根目录的请求。


$ b $ ('/',函数(req,res){
//返回一条消息
res.send('Welcome!')b

  ); 

});

将上面的代码片段放在 app.listen()和欢迎!消息将被显示。



为了在html文件中提供视图,您需要添加一个视图引擎,以便Express知道如何渲染文件。此外,需要配置视图属性,以便Express知道从哪里渲染视图。下面的例子使用 ejs 来处理纯html文件,而不用担心像 jade

  app.use('views',path.join(__ dirname +'/ views')); 
app.engine('html',require('ejs')。renderFile);
app.use('view engine','html');

现在我们可以使用 res 中的.html#res.renderrel =nofollow> render 功能。 / a>对象发送我们的index.html文件:

  app.get('/',function(req,res) {
res.render('index');
});

这是指南,介绍如何在Express中实现路由。


I am sure this is something small, but I am unable to figure it out. Node, nodemon and gulp use app.js to start my server so I am not sure why one way works while gulp does not. If I start my server using:

nodemon app.js or node app.js it starts fine and when I use the path localhost:1337 in the browser it does what it is supposed to do.

When I use the command:

gulp

it says that it started the server but when I navigate to localhost:1337 it does not display anything other than "Cannot GET /" and none of my sources are showing in the inspector. I still get the message that "the port is listening" as indicated in my app.js console.log. My gulpfile is as follows:

var gulp = require('gulp');
var nodemon = require('gulp-nodemon');

gulp.task('nodemon', function(){
    nodemon({
    script: './app/app.js'
    })

});

gulp.task('watch', function(){
    gulp.watch('*.js', ['nodemon']);
});

gulp.task('default', ['nodemon']);


my app.js file is:

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

app.use(express.static('../views'));
app.use(express.static('../bower_components/angular'));
app.use(express.static('./directives'));

app.listen(1337);
console.log("The port is listening");

解决方案

The 'Cannot GET /' being displayed when you go to localhost:1337 is Express's generic message when a request is made against a route that is undefined. A route needs to be defined for the pages that you want to serve. You need to add a Route to handle the request to the root of your app at http://localhost:1337/.

app.get('/', function(req, res) {
    // return back a message
    res.send('Welcome!');

});

Place the above snippet above app.listen() and the message "Welcome!" will be displayed.

To serve a view in an html file you need to add a view engine so Express knows how to render the file. Additionally, the views property needs to be configured so Express knows where to render your views from. The example below uses ejs to just handle plain html files and not worry about a template engine like jade.

app.use('views', path.join(__dirname + '/views'));
app.engine('html', require('ejs').renderFile);
app.use('view engine', 'html');

Now we can use the render function on the res object to send our index.html file:

app.get('/', function(req, res) {
  res.render('index');
});

This a guide from ExpressJS on how to implement routing in Express.

这篇关于节点启动我的服务器,吞咽也是如此,但吞咽并没有找到任何路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆